summaryrefslogtreecommitdiff
path: root/Apache/Solr/Service/Balancer.php
blob: cad57e52b7941af95857511a927b545ad83f6cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
<?php
/**
 * Copyright (c) 2007-2011, Servigistics, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  - Neither the name of Servigistics, Inc. nor the names of
 *    its contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
 * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
 * @version $Id: Balancer.php 54 2011-02-04 16:29:18Z donovan.jimenez $
 *
 * @package Apache
 * @subpackage Solr
 * @author Donovan Jimenez <djimenez@conduit-it.com>, Dan Wolfe
 */

// See Issue #1 (http://code.google.com/p/solr-php-client/issues/detail?id=1)
// Doesn't follow typical include path conventions, but is more convenient for users
require_once(dirname(dirname(__FILE__)) . '/Service.php');

require_once(dirname(dirname(__FILE__)) . '/NoServiceAvailableException.php');

/**
 * Reference Implementation for using multiple Solr services in a distribution. Functionality
 * includes:
 * 	routing of read / write operations
 * 	failover (on selection) for multiple read servers
 */
class Apache_Solr_Service_Balancer
{
	/**
	 * SVN Revision meta data for this class
	 */
	const SVN_REVISION = '$Revision: 54 $';

	/**
	 * SVN ID meta data for this class
	 */
	const SVN_ID = '$Id: Balancer.php 54 2011-02-04 16:29:18Z donovan.jimenez $';

	protected $_createDocuments = true;

	protected $_readableServices = array();
	protected $_writeableServices = array();

	protected $_currentReadService = null;
	protected $_currentWriteService = null;

	protected $_readPingTimeout = 2;
	protected $_writePingTimeout = 4;

	// Configuration for server selection backoff intervals
	protected $_useBackoff = false;		// Set to true to use more resillient write server selection
	protected $_backoffLimit = 600;		// 10 minute default maximum
	protected $_backoffEscalation = 2.0; 	// Rate at which to increase backoff period
	protected $_defaultBackoff = 2.0;		// Default backoff interval

	/**
	 * Escape a value for special query characters such as ':', '(', ')', '*', '?', etc.
	 *
	 * NOTE: inside a phrase fewer characters need escaped, use {@link Apache_Solr_Service::escapePhrase()} instead
	 *
	 * @param string $value
	 * @return string
	 */
	static public function escape($value)
	{
		return Apache_Solr_Service::escape($value);
	}

	/**
	 * Escape a value meant to be contained in a phrase for special query characters
	 *
	 * @param string $value
	 * @return string
	 */
	static public function escapePhrase($value)
	{
		return Apache_Solr_Service::escapePhrase($value);
	}

	/**
	 * Convenience function for creating phrase syntax from a value
	 *
	 * @param string $value
	 * @return string
	 */
	static public function phrase($value)
	{
		return Apache_Solr_Service::phrase($value);
	}

	/**
	 * Constructor. Takes arrays of read and write service instances or descriptions
	 *
	 * @param array $readableServices
	 * @param array $writeableServices
	 */
	public function __construct($readableServices = array(), $writeableServices = array())
	{
		//setup readable services
		foreach ($readableServices as $service)
		{
			$this->addReadService($service);
		}

		//setup writeable services
		foreach ($writeableServices as $service)
		{
			$this->addWriteService($service);
		}
	}

	public function setReadPingTimeout($timeout)
	{
		$this->_readPingTimeout = $timeout;
	}

	public function setWritePingTimeout($timeout)
	{
		$this->_writePingTimeout = $timeout;
	}

	public function setUseBackoff($enable)
	{
		$this->_useBackoff = $enable;
	}

	/**
	 * Generates a service ID
	 *
	 * @param string $host
	 * @param integer $port
	 * @param string $path
	 * @return string
	 */
	protected function _getServiceId($host, $port, $path)
	{
		return $host . ':' . $port . $path;
	}

	/**
	 * Adds a service instance or service descriptor (if it is already
	 * not added)
	 *
	 * @param mixed $service
	 *
	 * @throws Apache_Solr_InvalidArgumentException If service descriptor is not valid
	 */
	public function addReadService($service)
	{
		if ($service instanceof Apache_Solr_Service)
		{
			$id = $this->_getServiceId($service->getHost(), $service->getPort(), $service->getPath());

			$this->_readableServices[$id] = $service;
		}
		else if (is_array($service))
		{
			if (isset($service['host']) && isset($service['port']) && isset($service['path']))
			{
				$id = $this->_getServiceId((string)$service['host'], (int)$service['port'], (string)$service['path']);

				$this->_readableServices[$id] = $service;
			}
			else
			{
				throw new Apache_Solr_InvalidArgumentException('A Readable Service description array does not have all required elements of host, port, and path');
			}
		}
	}

	/**
	 * Removes a service instance or descriptor from the available services
	 *
	 * @param mixed $service
	 *
	 * @throws Apache_Solr_InvalidArgumentException If service descriptor is not valid
	 */
	public function removeReadService($service)
	{
		$id = '';

		if ($service instanceof Apache_Solr_Service)
		{
			$id = $this->_getServiceId($service->getHost(), $service->getPort(), $service->getPath());
		}
		else if (is_array($service))
		{
			if (isset($service['host']) && isset($service['port']) && isset($service['path']))
			{
				$id = $this->_getServiceId((string)$service['host'], (int)$service['port'], (string)$service['path']);
			}
			else
			{
				throw new Apache_Solr_InvalidArgumentException('A Readable Service description array does not have all required elements of host, port, and path');
			}
		}
		else if (is_string($service))
		{
			$id = $service;
		}

		if ($id && isset($this->_readableServices[$id]))
		{
			unset($this->_readableServices[$id]);
		}
	}

	/**
	 * Adds a service instance or service descriptor (if it is already
	 * not added)
	 *
	 * @param mixed $service
	 *
	 * @throws Apache_Solr_InvalidArgumentException If service descriptor is not valid
	 */
	public function addWriteService($service)
	{
		if ($service instanceof Apache_Solr_Service)
		{
			$id = $this->_getServiceId($service->getHost(), $service->getPort(), $service->getPath());

			$this->_writeableServices[$id] = $service;
		}
		else if (is_array($service))
		{
			if (isset($service['host']) && isset($service['port']) && isset($service['path']))
			{
				$id = $this->_getServiceId((string)$service['host'], (int)$service['port'], (string)$service['path']);

				$this->_writeableServices[$id] = $service;
			}
			else
			{
				throw new Apache_Solr_InvalidArgumentException('A Writeable Service description array does not have all required elements of host, port, and path');
			}
		}
	}

	/**
	 * Removes a service instance or descriptor from the available services
	 *
	 * @param mixed $service
	 *
	 * @throws Apache_Solr_InvalidArgumentException If service descriptor is not valid
	 */
	public function removeWriteService($service)
	{
		$id = '';

		if ($service instanceof Apache_Solr_Service)
		{
			$id = $this->_getServiceId($service->getHost(), $service->getPort(), $service->getPath());
		}
		else if (is_array($service))
		{
			if (isset($service['host']) && isset($service['port']) && isset($service['path']))
			{
				$id = $this->_getServiceId((string)$service['host'], (int)$service['port'], (string)$service['path']);
			}
			else
			{
				throw new Apache_Solr_InvalidArgumentException('A Readable Service description array does not have all required elements of host, port, and path');
			}
		}
		else if (is_string($service))
		{
			$id = $service;
		}

		if ($id && isset($this->_writeableServices[$id]))
		{
			unset($this->_writeableServices[$id]);
		}
	}

	/**
	 * Iterate through available read services and select the first with a ping
	 * that satisfies configured timeout restrictions (or the default)
	 *
	 * @return Apache_Solr_Service
	 *
	 * @throws Apache_Solr_NoServiceAvailableException If there are no read services that meet requirements
	 */
	protected function _selectReadService($forceSelect = false)
	{
		if (!$this->_currentReadService || !isset($this->_readableServices[$this->_currentReadService]) || $forceSelect)
		{
			if ($this->_currentReadService && isset($this->_readableServices[$this->_currentReadService]) && $forceSelect)
			{
				// we probably had a communication error, ping the current read service, remove it if it times out
				if ($this->_readableServices[$this->_currentReadService]->ping($this->_readPingTimeout) === false)
				{
					$this->removeReadService($this->_currentReadService);
				}
			}

			if (count($this->_readableServices))
			{
				// select one of the read services at random
				$ids = array_keys($this->_readableServices);

				$id = $ids[rand(0, count($ids) - 1)];
				$service = $this->_readableServices[$id];

				if (is_array($service))
				{
					//convert the array definition to a client object
					$service = new Apache_Solr_Service($service['host'], $service['port'], $service['path']);
					$this->_readableServices[$id] = $service;
				}

				$service->setCreateDocuments($this->_createDocuments);
				$this->_currentReadService = $id;
			}
			else
			{
				throw new Apache_Solr_NoServiceAvailableException('No read services were available');
			}
		}

		return $this->_readableServices[$this->_currentReadService];
	}

	/**
	 * Iterate through available write services and select the first with a ping
	 * that satisfies configured timeout restrictions (or the default)
	 *
	 * @return Apache_Solr_Service
	 *
	 * @throws Apache_Solr_NoServiceAvailableException If there are no write services that meet requirements
	 */
	protected function _selectWriteService($forceSelect = false)
	{
		if($this->_useBackoff)
		{
			return $this->_selectWriteServiceSafe($forceSelect);
		}

		if (!$this->_currentWriteService || !isset($this->_writeableServices[$this->_currentWriteService]) || $forceSelect)
		{
			if ($this->_currentWriteService && isset($this->_writeableServices[$this->_currentWriteService]) && $forceSelect)
			{
				// we probably had a communication error, ping the current read service, remove it if it times out
				if ($this->_writeableServices[$this->_currentWriteService]->ping($this->_writePingTimeout) === false)
				{
					$this->removeWriteService($this->_currentWriteService);
				}
			}

			if (count($this->_writeableServices))
			{
				// select one of the read services at random
				$ids = array_keys($this->_writeableServices);

				$id = $ids[rand(0, count($ids) - 1)];
				$service = $this->_writeableServices[$id];

				if (is_array($service))
				{
					//convert the array definition to a client object
					$service = new Apache_Solr_Service($service['host'], $service['port'], $service['path']);
					$this->_writeableServices[$id] = $service;
				}

				$this->_currentWriteService = $id;
			}
			else
			{
				throw new Apache_Solr_NoServiceAvailableException('No write services were available');
			}
		}

		return $this->_writeableServices[$this->_currentWriteService];
	}

	/**
	 * Iterate through available write services and select the first with a ping
	 * that satisfies configured timeout restrictions (or the default).  The
	 * timeout period will increase until a connection is made or the limit is
	 * reached.   This will allow for increased reliability with heavily loaded
	 * server(s).
	 *
	 * @return Apache_Solr_Service
	 *
	 * @throws Apache_Solr_NoServiceAvailableException If there are no write services that meet requirements
	 */

	protected function _selectWriteServiceSafe($forceSelect = false)
	{
		if (!$this->_currentWriteService || !isset($this->_writeableServices[$this->_currentWriteService]) || $forceSelect)
		{
			if (count($this->_writeableServices))
			{
				$backoff = $this->_defaultBackoff;

				do {
					// select one of the read services at random
					$ids = array_keys($this->_writeableServices);

					$id = $ids[rand(0, count($ids) - 1)];
					$service = $this->_writeableServices[$id];

					if (is_array($service))
					{
						//convert the array definition to a client object
						$service = new Apache_Solr_Service($service['host'], $service['port'], $service['path']);
						$this->_writeableServices[$id] = $service;
					}

					$this->_currentWriteService = $id;

					$backoff *= $this->_backoffEscalation;

					if($backoff > $this->_backoffLimit)
					{
						throw new Apache_Solr_NoServiceAvailableException('No write services were available.  All timeouts exceeded.');
					}

				} while($this->_writeableServices[$this->_currentWriteService]->ping($backoff) === false);
			}
			else
			{
				throw new Apache_Solr_NoServiceAvailableException('No write services were available');
			}
		}

		return $this->_writeableServices[$this->_currentWriteService];
	}

	/**
	 * Set the create documents flag. This determines whether {@link Apache_Solr_Response} objects will
	 * parse the response and create {@link Apache_Solr_Document} instances in place.
	 *
	 * @param boolean $createDocuments
	 */
	public function setCreateDocuments($createDocuments)
	{
		$this->_createDocuments = (bool) $createDocuments;

		// set on current read service
		if ($this->_currentReadService)
		{
			$service = $this->_selectReadService();
			$service->setCreateDocuments($createDocuments);
		}
	}

	/**
	 * Get the current state of teh create documents flag.
	 *
	 * @return boolean
	 */
	public function getCreateDocuments()
	{
		return $this->_createDocuments;
	}
	
	/**
	 * Raw Add Method. Takes a raw post body and sends it to the update service.  Post body
	 * should be a complete and well formed "add" xml document.
	 *
	 * @param string $rawPost
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function add($rawPost)
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->add($rawPost);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}

	/**
	 * Add a Solr Document to the index
	 *
	 * @param Apache_Solr_Document $document
	 * @param boolean $allowDups
	 * @param boolean $overwritePending
	 * @param boolean $overwriteCommitted
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function addDocument(Apache_Solr_Document $document, $allowDups = false, $overwritePending = true, $overwriteCommitted = true)
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->addDocument($document, $allowDups, $overwritePending, $overwriteCommitted);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}

	/**
	 * Add an array of Solr Documents to the index all at once
	 *
	 * @param array $documents Should be an array of Apache_Solr_Document instances
	 * @param boolean $allowDups
	 * @param boolean $overwritePending
	 * @param boolean $overwriteCommitted
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function addDocuments($documents, $allowDups = false, $overwritePending = true, $overwriteCommitted = true)
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->addDocuments($documents, $allowDups, $overwritePending, $overwriteCommitted);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}

	/**
	 * Send a commit command.  Will be synchronous unless both wait parameters are set
	 * to false.
	 *
	 * @param boolean $waitFlush
	 * @param boolean $waitSearcher
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function commit($optimize = true, $waitFlush = true, $waitSearcher = true, $timeout = 3600)
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->commit($optimize, $waitFlush, $waitSearcher, $timeout);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}

	/**
	 * Raw Delete Method. Takes a raw post body and sends it to the update service. Body should be
	 * a complete and well formed "delete" xml document
	 *
	 * @param string $rawPost
	 * @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function delete($rawPost, $timeout = 3600)
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->delete($rawPost, $timeout);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}

	/**
	 * Create a delete document based on document ID
	 *
	 * @param string $id
	 * @param boolean $fromPending
	 * @param boolean $fromCommitted
	 * @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function deleteById($id, $fromPending = true, $fromCommitted = true, $timeout = 3600)
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->deleteById($id, $fromPending, $fromCommitted, $timeout);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}

	/**
	 * Create and post a delete document based on multiple document IDs.
	 *
	 * @param array $ids Expected to be utf-8 encoded strings
	 * @param boolean $fromPending
	 * @param boolean $fromCommitted
	 * @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function deleteByMultipleIds($ids, $fromPending = true, $fromCommitted = true, $timeout = 3600)
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->deleteByMultipleId($ids, $fromPending, $fromCommitted, $timeout);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}

	/**
	 * Create a delete document based on a query and submit it
	 *
	 * @param string $rawQuery
	 * @param boolean $fromPending
	 * @param boolean $fromCommitted
	 * @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function deleteByQuery($rawQuery, $fromPending = true, $fromCommitted = true, $timeout = 3600)
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->deleteByQuery($rawQuery, $fromPending, $fromCommitted, $timeout);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}
	
	/**
	 * Use Solr Cell to extract document contents. See {@link http://wiki.apache.org/solr/ExtractingRequestHandler} for information on how
	 * to use Solr Cell and what parameters are available.
	 *
	 * NOTE: when passing an Apache_Solr_Document instance, field names and boosts will automatically be prepended by "literal." and "boost."
	 * as appropriate. Any keys from the $params array will NOT be treated this way. Any mappings from the document will overwrite key / value
	 * pairs in the params array if they have the same name (e.g. you pass a "literal.id" key and value in your $params array but you also
	 * pass in a document isntance with an "id" field" - the document's value(s) will take precedence).
	 *
	 * @param string $file Path to file to extract data from
	 * @param array $params optional array of key value pairs that will be sent with the post (see Solr Cell documentation)
	 * @param Apache_Solr_Document $document optional document that will be used to generate post parameters (literal.* and boost.* params)
	 * @param string $mimetype optional mimetype specification (for the file being extracted)
	 *
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_InvalidArgumentException if $file, $params, or $document are invalid.
	 */
	public function extract($file, $params = array(), $document = null, $mimetype = 'application/octet-stream')
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->extract($file, $params, $document, $mimetype);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}
	
	/**
	 * Use Solr Cell to extract document contents. See {@link http://wiki.apache.org/solr/ExtractingRequestHandler} for information on how
	 * to use Solr Cell and what parameters are available.
	 *
	 * NOTE: when passing an Apache_Solr_Document instance, field names and boosts will automatically be prepended by "literal." and "boost."
	 * as appropriate. Any keys from the $params array will NOT be treated this way. Any mappings from the document will overwrite key / value
	 * pairs in the params array if they have the same name (e.g. you pass a "literal.id" key and value in your $params array but you also
	 * pass in a document isntance with an "id" field" - the document's value(s) will take precedence).
	 *
	 * @param string $data Data that will be passed to Solr Cell
	 * @param array $params optional array of key value pairs that will be sent with the post (see Solr Cell documentation)
	 * @param Apache_Solr_Document $document optional document that will be used to generate post parameters (literal.* and boost.* params)
	 * @param string $mimetype optional mimetype specification (for the file being extracted)
	 *
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_InvalidArgumentException if $file, $params, or $document are invalid.
	 *
	 * @todo Should be using multipart/form-data to post parameter values, but I could not get my implementation to work. Needs revisisted.
	 */
	public function extractFromString($data, $params = array(), $document = null, $mimetype = 'application/octet-stream')
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->extractFromString($data, $params, $document, $mimetype);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}
	
	/**
	 * Send an optimize command.  Will be synchronous unless both wait parameters are set
	 * to false.
	 *
	 * @param boolean $waitFlush
	 * @param boolean $waitSearcher
	 * @param float $timeout Maximum expected duration of the optimize operation on the server (otherwise, will throw a communication exception)
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function optimize($waitFlush = true, $waitSearcher = true, $timeout = 3600)
	{
		$service = $this->_selectWriteService();

		do
		{
			try
			{
				return $service->optimize($waitFlush, $waitSearcher, $timeout);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectWriteService(true);
		} while ($service);

		return false;
	}

	/**
	 * Simple Search interface
	 *
	 * @param string $query The raw query string
	 * @param int $offset The starting offset for result documents
	 * @param int $limit The maximum number of result documents to return
	 * @param array $params key / value pairs for query parameters, use arrays for multivalued parameters
	 * @param string $method The HTTP method (Apache_Solr_Service::METHOD_GET or Apache_Solr_Service::METHOD::POST)
	 * @return Apache_Solr_Response
	 *
	 * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
	 */
	public function search($query, $offset = 0, $limit = 10, $params = array(), $method = Apache_Solr_Service::METHOD_GET)
	{
		$service = $this->_selectReadService();

		do
		{
			try
			{
				return $service->search($query, $offset, $limit, $params, $method);
			}
			catch (Apache_Solr_HttpTransportException $e)
			{
				if ($e->getCode() != 0) //IF NOT COMMUNICATION ERROR
				{
					throw $e;
				}
			}

			$service = $this->_selectReadService(true);
		} while ($service);

		return false;
	}
}