Skip to content

Commit

Permalink
couchbase: allow to configure bucket replicas and default to 0.
Browse files Browse the repository at this point in the history
This change allows the user to configure the number of bucket replicas
and defaults it to 0 since most of the time the user ever only needs
no replicas since it is a single container.

Note: technically this is a behavioral change since before the number
of replicas was 1 (if not defined) but this causes issues when
testing transactions (as seen in testcontainers#5835).

Fixes testcontainers#5835
  • Loading branch information
daschl committed Sep 14, 2022
1 parent 0d368f7 commit 9251220
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,32 @@ public class BucketDefinition {

private int quota = 100;

private int numReplicas = 0;

public BucketDefinition(final String name) {
this.name = name;
}

/**
* Allows to configure the number of replicas on a bucket (defaults to 0).
* <p>
* By default the bucket is initialized with 0 replicas since only a single container is launched. Modifying
* this value can still be useful in some test scenarios (i.e. to test failures with the wrong number of replicas
* and durability requirements on operations).
* <p>
* Couchbase buckets can have a maximum of three replicas configured.
*
* @param numReplicas the number of replicas to configure.
* @return this {@link BucketDefinition} for chaining purposes.
*/
public BucketDefinition withReplicas(final int numReplicas) {
if (numReplicas < 0 || numReplicas > 3) {
throw new IllegalArgumentException("The number of replicas must be between 0 and 3 (inclusive)");
}
this.numReplicas = numReplicas;
return this;
}

/**
* Enables flush for this bucket (disabled by default).
*
Expand All @@ -45,9 +67,9 @@ public BucketDefinition withFlushEnabled(final boolean flushEnabled) {
}

/**
* Sets a custom bucket quota (100MB by default).
* Sets a custom bucket quota (100MiB by default).
*
* @param quota the quota to set for the bucket.
* @param quota the quota to set for the bucket in mebibytes.
* @return this {@link BucketDefinition} for chaining purposes.
*/
public BucketDefinition withQuota(final int quota) {
Expand Down Expand Up @@ -84,4 +106,8 @@ public boolean hasPrimaryIndex() {
public int getQuota() {
return quota;
}

public int getNumReplicas() {
return numReplicas;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ private void createBuckets() {
.add("name", bucket.getName())
.add("ramQuotaMB", Integer.toString(bucket.getQuota()))
.add("flushEnabled", bucket.hasFlushEnabled() ? "1" : "0")
.add("replicaNumber", Integer.toString(bucket.getNumReplicas()))
.build(),
true
);
Expand Down

0 comments on commit 9251220

Please sign in to comment.