io.datakernel.crdt.CrdtRepartitionController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-crdt Show documentation
Show all versions of datakernel-crdt Show documentation
Conflict-free replicated data type implementation for DataKernel Framework.
/*
* Copyright (C) 2015-2019 SoftIndex LLC.
*
* 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.datakernel.crdt;
import io.datakernel.async.process.Cancellable;
import io.datakernel.common.exception.StacklessException;
import io.datakernel.datastream.StreamConsumer;
import io.datakernel.datastream.StreamDataAcceptor;
import io.datakernel.datastream.StreamSupplier;
import io.datakernel.datastream.processor.StreamMapSplitter;
import io.datakernel.eventloop.Eventloop;
import io.datakernel.eventloop.jmx.EventloopJmxMBeanEx;
import io.datakernel.promise.Promise;
import io.datakernel.promise.Promises;
import org.jetbrains.annotations.NotNull;
public final class CrdtRepartitionController, K extends Comparable, S> implements EventloopJmxMBeanEx {
private final I localPartitionId;
private final CrdtStorage localClient;
private final CrdtStorageCluster cluster;
public CrdtRepartitionController(I localPartitionId, CrdtStorage localClient, CrdtStorageCluster cluster) {
this.localClient = localClient;
this.cluster = cluster;
this.localPartitionId = localPartitionId;
}
public static , K extends Comparable, S> CrdtRepartitionController create(CrdtStorageCluster cluster, I localPartitionId) {
return new CrdtRepartitionController<>(localPartitionId, cluster.getClients().get(localPartitionId), cluster);
}
@NotNull
@Override
public Eventloop getEventloop() {
return cluster.getEventloop();
}
public Promise repartition() {
return Promises.toTuple(cluster.upload().toTry(), localClient.remove().toTry(), localClient.download().toTry())
.then(all -> {
if (all.getValue1().isSuccess() && all.getValue2().isSuccess() && all.getValue3().isSuccess()) {
StreamConsumer> cluster = all.getValue1().get();
StreamConsumer remover = all.getValue2().get();
StreamSupplier> downloader = all.getValue3().get();
int index = this.cluster.getOrderedIds().indexOf(localPartitionId);
StreamMapSplitter> splitter = StreamMapSplitter.create(
(data, acceptors) -> {
StreamDataAcceptor