All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.MapWriteBehindTask Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
/**
 * Copyright (c) 2013-2021 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.redisson.api.MapOptions;
import org.redisson.api.RFuture;
import org.redisson.api.RQueue;
import org.redisson.command.CommandAsyncExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author Nikita Koksharov
 *
 */
public class MapWriteBehindTask {

    private static final Logger log = LoggerFactory.getLogger(MapWriteBehindTask.class);
    
    private final AtomicBoolean isStarted = new AtomicBoolean();
    private final RQueue writeBehindTasks;
    private final CommandAsyncExecutor commandExecutor;
    private final MapOptions options;
    
    public MapWriteBehindTask(String name, CommandAsyncExecutor commandExecutor, MapOptions options) {
        super();
        this.commandExecutor = commandExecutor;
        this.options = (MapOptions) options;
        String queueName = RedissonObject.suffixName(name, "write-behind-queue");
        this.writeBehindTasks = new RedissonQueue<>(commandExecutor, queueName, null);
    }

    public void start() {
        if (!isStarted.compareAndSet(false, true)) {
            return;
        }

        enqueueTask();
    }

    private void pollTask(Map addedMap, List deletedKeys) {
        RFuture future = writeBehindTasks.pollAsync();
        future.onComplete((task, e) -> {
            if (e != null) {
                log.error(e.getMessage(), e);

                enqueueTask();
                return;
            }

            commandExecutor.getConnectionManager().getExecutor().execute(() -> {
                if (task != null) {
                    if (task instanceof MapWriterTask.Remove) {
                            for (Object key : task.getKeys()) {
                                try {
                                    deletedKeys.add(key);
                                    if (deletedKeys.size() == options.getWriteBehindBatchSize()) {
                                        options.getWriter().delete(deletedKeys);
                                        deletedKeys.clear();

                                    }
                                } catch (Exception exception) {
                                    log.error("Unable to delete keys: " + deletedKeys, exception);
                                }
                            }
                    } else {
                        for (Entry entry : task.getMap().entrySet()) {
                            try {
                                addedMap.put(entry.getKey(), entry.getValue());
                                if (addedMap.size() == options.getWriteBehindBatchSize()) {
                                    options.getWriter().write(addedMap);
                                    addedMap.clear();
                                }
                            } catch (Exception exception) {
                                log.error("Unable to add keys: " + addedMap, exception);
                            }
                        }
                    }

                    pollTask(addedMap, deletedKeys);
                } else {
                    try {
                        if (!deletedKeys.isEmpty()) {
                            options.getWriter().delete(deletedKeys);
                            deletedKeys.clear();
                        }
                    } catch (Exception exception) {
                        log.error("Unable to delete keys: " + deletedKeys, exception);
                    }
                    try {
                        if (!addedMap.isEmpty()) {
                            options.getWriter().write(addedMap);
                            addedMap.clear();
                        }
                    } catch (Exception exception) {
                        log.error("Unable to add keys: " + addedMap, exception);
                    }

                    enqueueTask();
                }
            });
        });
    }

    private void enqueueTask() {
        if (!isStarted.get()) {
            return;
        }

        commandExecutor.getConnectionManager().newTimeout(t -> {
            Map addedMap = new LinkedHashMap<>();
            List deletedKeys = new ArrayList<>();
            pollTask(addedMap, deletedKeys);
        }, options.getWriteBehindDelay(), TimeUnit.MILLISECONDS);
    }

    public void addTask(MapWriterTask task) {
        writeBehindTasks.addAsync(task);
    }

    public void stop() {
        isStarted.set(false);
    }
}