org.redisson.api.BatchOptions Java Examples

The following examples show how to use org.redisson.api.BatchOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedissonReferenceReactiveTest.java    From redisson with Apache License 2.0 8 votes vote down vote up
@Test
public void testReactiveToNormal() throws InterruptedException {
    RBatchReactive batch = redisson.createBatch(BatchOptions.defaults());
    RBucketReactive<Object> b1 = batch.getBucket("b1");
    RBucketReactive<Object> b2 = batch.getBucket("b2");
    RBucketReactive<Object> b3 = batch.getBucket("b3");
    b2.set(b3);
    b1.set(b2);
    b3.set(b1);
    sync(batch.execute());

    RedissonClient lredisson = Redisson.create(redisson.getConfig());
    RBatch b = lredisson.createBatch(BatchOptions.defaults());
    b.getBucket("b1").getAsync();
    b.getBucket("b2").getAsync();
    b.getBucket("b3").getAsync();
    List<RBucket> result = (List<RBucket>)b.execute().getResponses();
    assertEquals("b2", result.get(0).getName());
    assertEquals("b3", result.get(1).getName());
    assertEquals("b1", result.get(2).getName());

    lredisson.shutdown();
}
 
Example #2
Source File: RedissonReferenceReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testBatch() throws InterruptedException {
    RBatchReactive batch = redisson.createBatch(BatchOptions.defaults());
    RBucketReactive<Object> b1 = batch.getBucket("b1");
    RBucketReactive<Object> b2 = batch.getBucket("b2");
    RBucketReactive<Object> b3 = batch.getBucket("b3");
    b2.set(b3);
    b1.set(b2);
    b3.set(b1);
    sync(batch.execute());

    batch = redisson.createBatch(BatchOptions.defaults());
    batch.getBucket("b1").get();
    batch.getBucket("b2").get();
    batch.getBucket("b3").get();
    List<RBucketReactive> result = (List<RBucketReactive>) sync(batch.execute()).getResponses();
    assertEquals("b2", result.get(0).getName());
    assertEquals("b3", result.get(1).getName());
    assertEquals("b1", result.get(2).getName());
}
 
Example #3
Source File: RedisCommonBatchExecutor.java    From redisson with Apache License 2.0 6 votes vote down vote up
public RedisCommonBatchExecutor(NodeSource source, RPromise<Void> mainPromise, 
        ConnectionManager connectionManager, BatchOptions options, Entry entry, AtomicInteger slots) {
    super(entry.isReadOnlyMode(), source, null, null, null, mainPromise, true, connectionManager, null);
    this.options = options;
    this.entry = entry;
    this.slots = slots;
    
    if (options.getRetryAttempts() > 0) {
        this.attempts = options.getRetryAttempts();
    }
    if (options.getRetryInterval() > 0) {
        this.retryInterval  = options.getRetryInterval();
    }
    if (options.getResponseTimeout() > 0) {
        this.responseTimeout = options.getResponseTimeout();
    }
    
}
 
Example #4
Source File: RedissonTransaction.java    From redisson with Apache License 2.0 6 votes vote down vote up
private void enableLocalCache(String requestId, Map<HashKey, HashValue> hashes) {
    if (hashes.isEmpty()) {
        return;
    }
    
    RedissonBatch publishBatch = new RedissonBatch(null, commandExecutor.getConnectionManager(), BatchOptions.defaults());
    for (Entry<HashKey, HashValue> entry : hashes.entrySet()) {
        String name = RedissonObject.suffixName(entry.getKey().getName(), RedissonLocalCachedMap.TOPIC_SUFFIX);
        RTopicAsync topic = publishBatch.getTopic(name, LocalCachedMessageCodec.INSTANCE);
        LocalCachedMapEnable msg = new LocalCachedMapEnable(requestId, entry.getValue().getKeyIds().toArray(new byte[entry.getValue().getKeyIds().size()][]));
        topic.publishAsync(msg);
    }
    
    try {
        publishBatch.execute();
    } catch (Exception e) {
        // skip it. Disabled local cache entries are enabled once reach timeout.
    }
}
 
Example #5
Source File: AbstractRedisSetWrapper.java    From geowave with Apache License 2.0 5 votes vote down vote up
@SuppressFBWarnings(justification = "This is intentional to avoid unnecessary sync")
protected A getCurrentAsyncCollection() {
  // avoid synchronization if unnecessary by checking for null outside
  // synchronized block
  if (currentAsync == null) {
    synchronized (this) {
      // check again within synchronized block
      if (currentAsync == null) {
        currentBatch = client.createBatch(BatchOptions.defaults());
        currentAsync = initAsyncCollection(currentBatch, setName, codec);
      }
    }
  }
  return currentAsync;
}
 
Example #6
Source File: RedisQueuedBatchExecutor.java    From redisson with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ParameterNumber")
public RedisQueuedBatchExecutor(boolean readOnlyMode, NodeSource source, Codec codec, RedisCommand<V> command,
        Object[] params, RPromise<R> mainPromise, boolean ignoreRedirect, ConnectionManager connectionManager,
        RedissonObjectBuilder objectBuilder, ConcurrentMap<MasterSlaveEntry, Entry> commands,
        ConcurrentMap<MasterSlaveEntry, ConnectionEntry> connections, BatchOptions options, AtomicInteger index,
        AtomicBoolean executed, AsyncSemaphore semaphore) {
    super(readOnlyMode, source, codec, command, params, mainPromise, ignoreRedirect, connectionManager, objectBuilder,
            commands, options, index, executed);
    
    this.connections = connections;
    this.semaphore = semaphore;
}
 
Example #7
Source File: RedisBatchExecutor.java    From redisson with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ParameterNumber")
public RedisBatchExecutor(boolean readOnlyMode, NodeSource source, Codec codec, RedisCommand<V> command,
        Object[] params, RPromise<R> mainPromise, boolean ignoreRedirect, ConnectionManager connectionManager,
        RedissonObjectBuilder objectBuilder, ConcurrentMap<MasterSlaveEntry, Entry> commands,
        BatchOptions options, AtomicInteger index,
        AtomicBoolean executed) {
    super(readOnlyMode, source, codec, command, params, mainPromise, ignoreRedirect, connectionManager, objectBuilder,
            commands, options, index, executed);
}
 
Example #8
Source File: BaseRedisBatchExecutor.java    From redisson with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ParameterNumber")
public BaseRedisBatchExecutor(boolean readOnlyMode, NodeSource source, Codec codec, RedisCommand<V> command,
        Object[] params, RPromise<R> mainPromise, boolean ignoreRedirect,
        ConnectionManager connectionManager, RedissonObjectBuilder objectBuilder, 
        ConcurrentMap<MasterSlaveEntry, Entry> commands,
        BatchOptions options, AtomicInteger index, AtomicBoolean executed) {
    
    super(readOnlyMode, source, codec, command, params, mainPromise, ignoreRedirect, connectionManager,
            objectBuilder);
    this.commands = commands;
    this.options = options;
    this.index = index;
    this.executed = executed;
}
 
Example #9
Source File: RedissonTransaction.java    From redisson with Apache License 2.0 5 votes vote down vote up
private RFuture<BatchResult<?>> enableLocalCacheAsync(String requestId, Map<HashKey, HashValue> hashes) {
    if (hashes.isEmpty()) {
        return RedissonPromise.newSucceededFuture(null);
    }
    
    RedissonBatch publishBatch = new RedissonBatch(null, commandExecutor.getConnectionManager(), BatchOptions.defaults());
    for (Entry<HashKey, HashValue> entry : hashes.entrySet()) {
        String name = RedissonObject.suffixName(entry.getKey().getName(), RedissonLocalCachedMap.TOPIC_SUFFIX);
        RTopicAsync topic = publishBatch.getTopic(name, LocalCachedMessageCodec.INSTANCE);
        LocalCachedMapEnable msg = new LocalCachedMapEnable(requestId, entry.getValue().getKeyIds().toArray(new byte[entry.getValue().getKeyIds().size()][]));
        topic.publishAsync(msg);
    }
    
    return publishBatch.executeAsync();
}
 
Example #10
Source File: RedissonTransaction.java    From redisson with Apache License 2.0 5 votes vote down vote up
private BatchOptions createOptions() {
    MasterSlaveEntry entry = commandExecutor.getConnectionManager().getEntrySet().iterator().next();
    int syncSlaves = entry.getAvailableSlaves();

    BatchOptions batchOptions = BatchOptions.defaults()
            .syncSlaves(syncSlaves, options.getSyncTimeout(), TimeUnit.MILLISECONDS)
            .responseTimeout(options.getResponseTimeout(), TimeUnit.MILLISECONDS)
            .retryAttempts(options.getRetryAttempts())
            .retryInterval(options.getRetryInterval(), TimeUnit.MILLISECONDS)
            .executionMode(BatchOptions.ExecutionMode.IN_MEMORY_ATOMIC);
    return batchOptions;
}
 
Example #11
Source File: RedissonSessionRepository.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public String changeSessionId() {
    String oldId = delegate.getId();
    String id = delegate.changeSessionId();

    RBatch batch = redisson.createBatch(BatchOptions.defaults());
    batch.getBucket(getExpiredKey(oldId)).remainTimeToLiveAsync();
    batch.getBucket(getExpiredKey(oldId)).deleteAsync();
    batch.getMap(map.getName(), map.getCodec()).readAllMapAsync();
    batch.getMap(map.getName()).deleteAsync();

    BatchResult<?> res = batch.execute();
    List<?> list = res.getResponses();

    Long remainTTL = (Long) list.get(0);
    Map<String, Object> oldState = (Map<String, Object>) list.get(2);

    if (remainTTL == -2) {
        // Either:
        // - a parallel request also invoked changeSessionId() on this session, and the
        //   expiredKey for oldId had been deleted
        // - sessions do not expire
        remainTTL = delegate.getMaxInactiveInterval().toMillis();
    }

    RBatch batchNew = redisson.createBatch();
    batchNew.getMap(keyPrefix + id, map.getCodec()).putAllAsync(oldState);
    if (remainTTL > 0) {
        batchNew.getBucket(getExpiredKey(id)).setAsync("", remainTTL, TimeUnit.MILLISECONDS);
    }
    batchNew.execute();

    map = redisson.getMap(keyPrefix + id, map.getCodec());

    return id;
}
 
Example #12
Source File: RedissonLock.java    From redisson with Apache License 2.0 5 votes vote down vote up
private CommandBatchService createCommandBatchService() {
    if (commandExecutor instanceof CommandBatchService) {
        return (CommandBatchService) commandExecutor;
    }

    MasterSlaveEntry entry = commandExecutor.getConnectionManager().getEntry(getName());
    BatchOptions options = BatchOptions.defaults()
                            .syncSlaves(entry.getAvailableSlaves(), 1, TimeUnit.SECONDS);

    return new CommandBatchService(commandExecutor.getConnectionManager(), options);
}
 
Example #13
Source File: RedissonBatchRxTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters(name= "{index} - {0}")
public static Iterable<Object[]> data() {
    return Arrays.asList(new Object[][] {
        {BatchOptions.defaults().executionMode(ExecutionMode.IN_MEMORY)},
        {BatchOptions.defaults().executionMode(ExecutionMode.REDIS_WRITE_ATOMIC)}
        });
}
 
Example #14
Source File: RedissonBatchRxTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws IOException, InterruptedException {
    super.before();
    if (batchOptions.getExecutionMode() == ExecutionMode.IN_MEMORY) {
        batchOptions = BatchOptions.defaults().executionMode(ExecutionMode.IN_MEMORY);
    }
    if (batchOptions.getExecutionMode() == ExecutionMode.REDIS_WRITE_ATOMIC) {
        batchOptions = BatchOptions.defaults().executionMode(ExecutionMode.REDIS_WRITE_ATOMIC);
    }
}
 
Example #15
Source File: RedissonBatchRxTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectionLeakAfterError() throws InterruptedException {
    Config config = BaseTest.createConfig();
    config.useSingleServer()
            .setRetryInterval(100)
            .setTimeout(200)
            .setConnectionMinimumIdleSize(1).setConnectionPoolSize(1);

    RedissonRxClient redisson = Redisson.createRx(config);
    
    BatchOptions batchOptions = BatchOptions.defaults().executionMode(ExecutionMode.REDIS_WRITE_ATOMIC);
    RBatchRx batch = redisson.createBatch(batchOptions);
    for (int i = 0; i < 25000; i++) {
        batch.getBucket("test").set(123);
    }
    
    try {
        sync(batch.execute());
        Assert.fail();
    } catch (Exception e) {
        // skip
    }
    
    sync(redisson.getBucket("test3").set(4));
    assertThat(sync(redisson.getBucket("test3").get())).isEqualTo(4);
    
    batch = redisson.createBatch(batchOptions);
    batch.getBucket("test1").set(1);
    batch.getBucket("test2").set(2);
    sync(batch.execute());
    
    assertThat(sync(redisson.getBucket("test1").get())).isEqualTo(1);
    assertThat(sync(redisson.getBucket("test2").get())).isEqualTo(2);
    
    redisson.shutdown();
}
 
Example #16
Source File: CommandBatchService.java    From redisson with Apache License 2.0 4 votes vote down vote up
public BatchOptions getOptions() {
    return options;
}
 
Example #17
Source File: CommandBatchService.java    From redisson with Apache License 2.0 4 votes vote down vote up
public CommandBatchService(ConnectionManager connectionManager, BatchOptions options) {
    super(connectionManager);
    this.options = options;
}
 
Example #18
Source File: CommandRxBatchService.java    From redisson with Apache License 2.0 4 votes vote down vote up
public CommandRxBatchService(ConnectionManager connectionManager, BatchOptions options) {
    super(connectionManager);
    batchService = new CommandBatchService(connectionManager, options);
}
 
Example #19
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RBatch createBatch(BatchOptions options) {
  return redissonClient.createBatch(options);
}
 
Example #20
Source File: CommandReactiveBatchService.java    From redisson with Apache License 2.0 4 votes vote down vote up
public CommandReactiveBatchService(ConnectionManager connectionManager, BatchOptions options) {
    super(connectionManager);
    batchService = new CommandBatchService(connectionManager, options);
}
 
Example #21
Source File: RedissonTransaction.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RFuture<Void> commitAsync() {
    checkState();
    
    checkTimeout();
    
    BatchOptions batchOptions = createOptions();
    
    CommandBatchService transactionExecutor = new CommandBatchService(commandExecutor.getConnectionManager(), batchOptions);
    for (TransactionalOperation transactionalOperation : operations) {
        transactionalOperation.commit(transactionExecutor);
    }

    String id = generateId();
    RPromise<Void> result = new RedissonPromise<Void>();
    RFuture<Map<HashKey, HashValue>> future = disableLocalCacheAsync(id, localCaches, operations);
    future.onComplete((res, ex) -> {
        if (ex != null) {
            result.tryFailure(new TransactionException("Unable to execute transaction", ex));
            return;
        }
        
        Map<HashKey, HashValue> hashes = future.getNow();
        try {
            checkTimeout();
        } catch (TransactionTimeoutException e) {
            enableLocalCacheAsync(id, hashes);
            result.tryFailure(e);
            return;
        }
                        
        RFuture<BatchResult<?>> transactionFuture = transactionExecutor.executeAsync();
        transactionFuture.onComplete((r, exc) -> {
            if (exc != null) {
                result.tryFailure(new TransactionException("Unable to execute transaction", exc));
                return;
            }
            
            enableLocalCacheAsync(id, hashes);
            executed.set(true);
            
            result.trySuccess(null);
        });
    });
    return result;
}
 
Example #22
Source File: RedissonConnection.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public void openPipeline() {
    BatchOptions options = BatchOptions.defaults()
            .executionMode(ExecutionMode.IN_MEMORY);
    this.executorService = new CommandBatchService(redisson.getConnectionManager(), options);
}
 
Example #23
Source File: RedissonConnection.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public void openPipeline() {
    BatchOptions options = BatchOptions.defaults()
            .executionMode(ExecutionMode.IN_MEMORY);
    this.executorService = new CommandBatchService(redisson.getConnectionManager(), options);
}
 
Example #24
Source File: RedissonConnection.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public void openPipeline() {
    BatchOptions options = BatchOptions.defaults()
            .executionMode(ExecutionMode.IN_MEMORY);
    this.executorService = new CommandBatchService(redisson.getConnectionManager(), options);
}
 
Example #25
Source File: RedissonConnection.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public void openPipeline() {
    BatchOptions options = BatchOptions.defaults()
            .executionMode(ExecutionMode.IN_MEMORY);
    this.executorService = new CommandBatchService(redisson.getConnectionManager(), options);
}
 
Example #26
Source File: RedissonConnection.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public void openPipeline() {
    BatchOptions options = BatchOptions.defaults()
            .executionMode(ExecutionMode.IN_MEMORY);
    this.executorService = new CommandBatchService(redisson.getConnectionManager(), options);
}
 
Example #27
Source File: RedissonConnection.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public void openPipeline() {
    BatchOptions options = BatchOptions.defaults()
            .executionMode(ExecutionMode.IN_MEMORY);
    this.executorService = new CommandBatchService(redisson.getConnectionManager(), options);
}
 
Example #28
Source File: RedissonConnection.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public void openPipeline() {
    BatchOptions options = BatchOptions.defaults()
            .executionMode(ExecutionMode.IN_MEMORY);
    this.executorService = new CommandBatchService(redisson.getConnectionManager(), options);
}