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

io.journalkeeper.journalstore.JournalStoreClient Maven / Gradle / Ivy

There is a newer version: 0.1.11
Show newest version
/**
 * 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 io.journalkeeper.journalstore;

import io.journalkeeper.core.BootStrap;
import io.journalkeeper.core.api.*;
import io.journalkeeper.core.api.transaction.TransactionalJournalStore;
import io.journalkeeper.core.entry.DefaultJournalEntryParser;
import io.journalkeeper.core.entry.internal.ReservedPartition;
import io.journalkeeper.exceptions.IndexOverflowException;
import io.journalkeeper.exceptions.IndexUnderflowException;
import io.journalkeeper.utils.event.EventWatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

/**
 * @author LiYue
 * Date: 2019-05-09
 */
public class JournalStoreClient implements PartitionedJournalStore, TransactionalJournalStore {
    private static final Logger logger = LoggerFactory.getLogger(JournalStoreClient.class);
    private final RaftClient raftClient;

    JournalStoreClient(RaftClient raftClient) {
        this.raftClient = raftClient;
    }

    /**
     * 初始化一个远程模式的客户端
     * @param servers 集群配置
     * @param properties 属性
     */
    public JournalStoreClient(List servers, Properties properties) {
        this(servers, new DefaultJournalEntryParser(), properties);
    }
    public JournalStoreClient(List servers, JournalEntryParser journalEntryParser, Properties properties) {
        BootStrap bootStrap = new BootStrap<>(
                servers,
                new ByteArraySerializer(),
                new LongSerializer(),
                new JournalStoreQuerySerializer(),
                new JournalStoreQueryResultSerializer(journalEntryParser),
                properties
                );
        raftClient = bootStrap.getClient();
    }

    @Override
    public CompletableFuture append(UpdateRequest updateRequest, boolean includeHeader, ResponseConfig responseConfig) {
        ReservedPartition.validatePartition(updateRequest.getPartition());
        return raftClient.update(updateRequest, includeHeader, responseConfig);
    }

    @Override
    public CompletableFuture append(UUID transactionId, UpdateRequest updateRequest, boolean includeHeader) {
        ReservedPartition.validatePartition(updateRequest.getPartition());
        return raftClient.update(transactionId, updateRequest, includeHeader);
    }

    @Override
    public CompletableFuture> append(List> updateRequests, boolean includeHeader, ResponseConfig responseConfig) {
        for (UpdateRequest updateRequest : updateRequests) {
            ReservedPartition.validatePartition(updateRequest.getPartition());
        }
        return raftClient.update(updateRequests, includeHeader, responseConfig);
    }

    @Override
    public CompletableFuture append(UUID transactionId, List> updateRequests, boolean includeHeader) {
        for (UpdateRequest updateRequest : updateRequests) {
            ReservedPartition.validatePartition(updateRequest.getPartition());
        }
        return raftClient.update(transactionId, updateRequests, includeHeader);
    }
    @Override
    public CompletableFuture> get(int partition, long index, int size) {
        ReservedPartition.validatePartition(partition);
        return raftClient.query(JournalStoreQuery.createQueryEntries(partition, index, size))
                .thenApply(result -> {
                    if(result.getCode() == JournalStoreQueryResult.CODE_SUCCESS) {
                        return result;
                    } else if (result.getCode() == JournalStoreQueryResult.CODE_UNDERFLOW) {
                        throw new CompletionException(new IndexUnderflowException());
                    } else if (result.getCode() == JournalStoreQueryResult.CODE_OVERFLOW) {
                        throw new CompletionException(new IndexOverflowException());
                    } else {
                        throw new CompletionException(new QueryJournalStoreException("Unknown exception"));
                    }
                })
                .thenApply(JournalStoreQueryResult::getEntries);
    }

    @Override
    public CompletableFuture> minIndices() {
        return raftClient.query(JournalStoreQuery.createQueryPartitions())
                .thenApply(JournalStoreQueryResult::getBoundaries)
                .thenApply(boundaries -> boundaries.entrySet().stream()
                    .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getMin()))
                );
    }

    @Override
    public CompletableFuture> maxIndices() {
        return raftClient.query(JournalStoreQuery.createQueryPartitions())
                .thenApply(JournalStoreQueryResult::getBoundaries)
                .thenApply(boundaries -> boundaries.entrySet().stream()
                        .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getMax()))
                );
    }

    @Override
    public CompletableFuture> listPartitions() {
        return raftClient.query(JournalStoreQuery.createQueryPartitions())
                .thenApply(JournalStoreQueryResult::getBoundaries)
                .thenApply(Map::keySet);
    }

    @Override
    public CompletableFuture queryIndex(int partition, long timestamp) {
        ReservedPartition.validatePartition(partition);
        return raftClient
                .query(JournalStoreQuery.createQueryIndex(partition, timestamp))
                .thenApply(result -> result.getCode() == JournalStoreQueryResult.CODE_SUCCESS ? result.getIndex(): -1L)
                .exceptionally(e -> {
                    logger.warn("Query index exception:", e);
                    return -1L;
                });
    }

    public void waitForClusterReady(long timeoutMs) throws InterruptedException, TimeoutException {
        raftClient.waitForClusterReady(timeoutMs);
    }

    public void waitForClusterReady() throws InterruptedException {
        raftClient.waitForClusterReady();
    }

    @Override
    public void watch(EventWatcher eventWatcher) {
        raftClient.watch(eventWatcher);
    }

    @Override
    public void unWatch(EventWatcher eventWatcher) {
        raftClient.watch(eventWatcher);
    }

    @Override
    public CompletableFuture createTransaction() {
        return raftClient.createTransaction();
    }

    @Override
    public CompletableFuture completeTransaction(UUID transactionId, boolean commitOrAbort) {
        return raftClient.completeTransaction(transactionId, commitOrAbort);
    }

    @Override
    public CompletableFuture> getOpeningTransactions() {
        return raftClient.getOpeningTransactions();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy