23
23
package com .couchbase .client ;
24
24
25
25
import com .couchbase .client .vbucket .ConfigurationException ;
26
- import com .couchbase .client .vbucket .ConfigurationProvider ;
26
+ import com .couchbase .client .vbucket .provider .BucketConfigurationProvider ;
27
+ import com .couchbase .client .vbucket .provider .ConfigurationProvider ;
27
28
import com .couchbase .client .vbucket .ConfigurationProviderHTTP ;
28
29
import com .couchbase .client .vbucket .CouchbaseNodeOrder ;
29
30
import com .couchbase .client .vbucket .Reconfigurable ;
@@ -204,8 +205,6 @@ public CouchbaseConnectionFactory(final List<URI> baseList,
204
205
}
205
206
206
207
private void initialize (List <URI > baseList , String bucket , String password ) {
207
- potentiallyRandomizeNodeList (baseList );
208
-
209
208
storedBaseList = new ArrayList <URI >();
210
209
for (URI bu : baseList ) {
211
210
if (!bu .isAbsolute ()) {
@@ -226,7 +225,7 @@ private void initialize(List<URI> baseList, String bucket, String password) {
226
225
this .bucket = bucket ;
227
226
pass = password ;
228
227
configurationProvider =
229
- new ConfigurationProviderHTTP (baseList , bucket , password );
228
+ new BucketConfigurationProvider (baseList , bucket , password , this );
230
229
}
231
230
232
231
@ Override
@@ -310,25 +309,15 @@ public CouchbaseNodeOrder getStreamingNodeOrder() {
310
309
}
311
310
312
311
public Config getVBucketConfig () {
313
- Bucket config = configurationProvider .getBucketConfiguration (bucket );
314
- if (config == null ) {
315
- throw new ConfigurationException ("Could not fetch valid configuration "
316
- + "from provided nodes. Stopping." );
317
- } else if (config .isNotUpdating ()) {
318
- LOGGER .warning ("Noticed bucket configuration to be disconnected, "
319
- + "will attempt to reconnect" );
320
- setConfigurationProvider (new ConfigurationProviderHTTP (storedBaseList ,
321
- bucket , pass ));
322
- }
323
- return configurationProvider .getBucketConfiguration (bucket ).getConfig ();
312
+ return configurationProvider .getConfig ().getConfig ();
324
313
}
325
314
326
315
public synchronized ConfigurationProvider getConfigurationProvider () {
327
316
return configurationProvider ;
328
317
}
329
318
330
319
protected void requestConfigReconnect (String bucketName , Reconfigurable rec ) {
331
- configurationProvider .markForResubscribe ( bucketName , rec );
320
+ configurationProvider .signalOutdated ( );
332
321
needsReconnect = true ;
333
322
}
334
323
@@ -367,7 +356,7 @@ void checkConfigUpdate() {
367
356
}
368
357
369
358
if (doingResubscribe .compareAndSet (false , true )) {
370
- resubConfigUpdate ();
359
+ getConfigurationProvider (). signalOutdated ();
371
360
} else {
372
361
LOGGER .log (Level .CONFIG , "Duplicate resubscribe for config updates"
373
362
+ " suppressed." );
@@ -379,15 +368,6 @@ void checkConfigUpdate() {
379
368
}
380
369
}
381
370
382
- /**
383
- * Resubscribe for configuration updates.
384
- */
385
- private synchronized void resubConfigUpdate () {
386
- LOGGER .log (Level .INFO , "Attempting to resubscribe for cluster config"
387
- + " updates." );
388
- resubExec .execute (new Resubscriber ());
389
- }
390
-
391
371
/**
392
372
* Checks if there have been more requests than allowed through
393
373
* maxConfigCheck in a 10 second period.
@@ -466,51 +446,6 @@ int getMaxConfigCheck() {
466
446
return maxConfigCheck ;
467
447
}
468
448
469
- private class Resubscriber implements Runnable {
470
-
471
- public void run () {
472
- String threadNameBase = "Couchbase/Resubscriber (Status: " ;
473
- Thread .currentThread ().setName (threadNameBase + "running)" );
474
- LOGGER .log (Level .CONFIG , "Resubscribing for {0} using base list {1}" ,
475
- new Object []{bucket , storedBaseList });
476
-
477
- long reconnectAttempt = 0 ;
478
- long backoffTime = 1000 ;
479
- long maxWaitTime = 10000 ;
480
- do {
481
- try {
482
- long waitTime = (reconnectAttempt ++)*backoffTime ;
483
- if (reconnectAttempt >= 10 ) {
484
- waitTime = maxWaitTime ;
485
- }
486
- LOGGER .log (Level .INFO , "Reconnect attempt {0}, waiting {1}ms" ,
487
- new Object []{reconnectAttempt , waitTime });
488
- Thread .sleep (waitTime );
489
-
490
- ConfigurationProvider oldConfigProvider = getConfigurationProvider ();
491
- Reconfigurable oldRec = oldConfigProvider .getReconfigurable ();
492
-
493
- ConfigurationProvider newConfigProvider =
494
- new ConfigurationProviderHTTP (storedBaseList , bucket , pass );
495
- newConfigProvider .subscribe (bucket , oldRec );
496
-
497
- setConfigurationProvider (newConfigProvider );
498
- oldConfigProvider .shutdown ();
499
-
500
- if (!doingResubscribe .compareAndSet (true , false )) {
501
- LOGGER .log (Level .WARNING ,
502
- "Could not reset from doing a resubscribe." );
503
- }
504
- } catch (Exception ex ) {
505
- LOGGER .log (Level .WARNING ,
506
- "Resubscribe attempt failed: " , ex );
507
- }
508
- } while (doingResubscribe .get ());
509
-
510
- Thread .currentThread ().setName (threadNameBase + "complete)" );
511
- }
512
- }
513
-
514
449
/**
515
450
* Returns a ClusterManager and initializes one if it does not exist.
516
451
* @return Returns an instance of a ClusterManager.
@@ -522,35 +457,6 @@ public ClusterManager getClusterManager() {
522
457
return clusterManager ;
523
458
}
524
459
525
- /**
526
- * Updates the stored base list with a new one based on the config.
527
- *
528
- * @param config
529
- */
530
- public void updateStoredBaseList (Config config ) {
531
- List <String > bucketServers = config .getRestEndpoints ();
532
- if (bucketServers .size () > 0 ) {
533
- List <URI > newList = new ArrayList <URI >();
534
- for (String bucketServer : bucketServers ) {
535
- try {
536
- newList .add (new URI (bucketServer ));
537
- } catch (URISyntaxException ex ) {
538
- getLogger ().warn ("Could not add node to updated bucket list because "
539
- + "of a parsing exception." );
540
- getLogger ().debug ("Could not parse list because: " + ex );
541
- }
542
- }
543
-
544
- if (nodeListsAreDifferent (storedBaseList , newList )) {
545
- getLogger ().info ("Replacing current streaming node list "
546
- + storedBaseList + " with " + newList );
547
- potentiallyRandomizeNodeList (newList );
548
- storedBaseList = newList ;
549
- getConfigurationProvider ().updateBaseListFromConfig (newList );
550
- }
551
- }
552
- }
553
-
554
460
/**
555
461
* Returns the current base list.
556
462
*
@@ -560,39 +466,6 @@ List<URI> getStoredBaseList() {
560
466
return storedBaseList ;
561
467
}
562
468
563
- /**
564
- * Randomizes the entries of the node list if needed.
565
- *
566
- * @param list the list to potentially randomize.
567
- */
568
- private void potentiallyRandomizeNodeList (List <URI > list ) {
569
- if (getStreamingNodeOrder ().equals (CouchbaseNodeOrder .ORDERED )) {
570
- return ;
571
- }
572
-
573
- Collections .shuffle (list );
574
- }
575
-
576
- /**
577
- * Check if two given node lists are different.
578
- *
579
- * @param left one node list
580
- * @param right the other node list
581
- * @return true if they are different, false otherwise.
582
- */
583
- private boolean nodeListsAreDifferent (List <URI > left , List <URI > right ) {
584
- if (left .size () != right .size ()) {
585
- return true ;
586
- }
587
-
588
- for (URI uri : left ) {
589
- if (!right .contains (uri )) {
590
- return true ;
591
- }
592
- }
593
- return false ;
594
- }
595
-
596
469
@ Override
597
470
public String toString () {
598
471
final StringBuilder sb = new StringBuilder ("CouchbaseConnectionFactory{" );
0 commit comments