Skip to content

Commit

Permalink
Add support for "balance" option
Browse files Browse the repository at this point in the history
Closes #462
  • Loading branch information
nickkkccc committed Jan 22, 2024
1 parent 14e051a commit 7eac932
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.tarantool.driver.api.space.options;

import io.tarantool.driver.api.space.options.crud.OperationWithBalanceOptions;
import io.tarantool.driver.api.space.options.crud.OperationWithBatchSizeOptions;
import io.tarantool.driver.api.space.options.crud.OperationWithBucketIdOptions;
import io.tarantool.driver.api.space.options.crud.OperationWithFieldsOptions;
Expand All @@ -16,5 +17,5 @@
*/
public interface SelectOptions<T extends SelectOptions<T>>
extends OperationWithBucketIdOptions<T>, OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T>,
OperationWithModeOptions<T>, OperationWithBatchSizeOptions<T> {
OperationWithModeOptions<T>, OperationWithBatchSizeOptions<T>, OperationWithBalanceOptions<T> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.tarantool.driver.api.space.options.crud;

import java.util.Optional;

import io.tarantool.driver.api.space.options.Options;
import io.tarantool.driver.api.space.options.Self;
import io.tarantool.driver.api.space.options.crud.enums.ProxyOption;

/**
* Base interface for all operation options that may have a configurable "balance" option.
*
* @author Belonogov Nikolay
*/
public interface OperationWithBalanceOptions<T extends OperationWithBalanceOptions<T>> extends Options, Self<T> {

/**
* Sets "balance" option value to true. If true then use replica according to vshard load balancing policy. Default
* value is false.
*
* @return this options instance.
*/
default T balance() {
addOption(ProxyOption.BALANCE, true);
return self();
}

/**
* @return "balance" option value.
*/
default Optional<Boolean> getBalance() {
return getOption(ProxyOption.BALANCE, Boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public enum ProxyOption {

AFTER("after"),

FIRST("first");
FIRST("first"),

BALANCE("balance");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.tarantool.driver.api.space.options.SelectOptions;
import io.tarantool.driver.api.space.TarantoolSpaceOperations;
import io.tarantool.driver.api.space.options.ProxySelectOptions;
import io.tarantool.driver.api.space.options.crud.enums.ProxyOption;
import io.tarantool.driver.api.tuple.DefaultTarantoolTupleFactory;
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.api.tuple.TarantoolTupleFactory;
Expand All @@ -28,6 +29,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Artyom Dubinin
Expand Down Expand Up @@ -174,4 +176,20 @@ public void withModeTest() throws ExecutionException, InterruptedException {
crudSelectOpts = client.eval("return crud_select_opts").get();
assertEquals(Mode.WRITE.value(), ((HashMap<?, ?>) crudSelectOpts.get(0)).get("mode"));
}

@Test
public void withBalanceTest() {
TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> operations =
client.space(TEST_SPACE_NAME);

SelectOptions<ProxySelectOptions> options = ProxySelectOptions.create().balance();

assertTrue(options.getBalance().isPresent());
assertTrue(options.getBalance().get());

operations.select(Conditions.any(), options).join();
List<?> crudSelectOpts = client.eval("return crud_select_opts").join();

assertEquals(true, ((HashMap<?, ?>) crudSelectOpts.get(0)).get(ProxyOption.BALANCE.toString()));
}
}

0 comments on commit 7eac932

Please sign in to comment.