io.journalkeeper.journalstore.JournalStoreClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of journalkeeper-journal-store Show documentation
Show all versions of journalkeeper-journal-store Show documentation
A journal storage cluster powered by JournalKeeper.
/**
* 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