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

io.etcd.jetcd.impl.MaintenanceImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2021 The jetcd authors
 *
 * 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.etcd.jetcd.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;

import io.etcd.jetcd.Maintenance;
import io.etcd.jetcd.api.*;
import io.etcd.jetcd.api.AlarmType;
import io.etcd.jetcd.maintenance.AlarmResponse;
import io.etcd.jetcd.maintenance.DefragmentResponse;
import io.etcd.jetcd.maintenance.HashKVResponse;
import io.etcd.jetcd.maintenance.MoveLeaderResponse;
import io.etcd.jetcd.maintenance.StatusResponse;
import io.grpc.stub.StreamObserver;

import static com.google.common.base.Preconditions.checkArgument;
import static io.etcd.jetcd.common.exception.EtcdExceptionFactory.toEtcdException;

/**
 * Implementation of maintenance client.
 */
final class MaintenanceImpl extends Impl implements Maintenance {
    private final VertxMaintenanceGrpc.MaintenanceVertxStub stub;

    MaintenanceImpl(ClientConnectionManager connectionManager) {
        super(connectionManager);

        this.stub = connectionManager().newStub(VertxMaintenanceGrpc::newVertxStub);
    }

    @Override
    public CompletableFuture listAlarms() {
        AlarmRequest alarmRequest = AlarmRequest.newBuilder()
            .setAlarm(AlarmType.NONE)
            .setAction(AlarmRequest.AlarmAction.GET)
            .setMemberID(0)
            .build();

        return completable(this.stub.alarm(alarmRequest), AlarmResponse::new);
    }

    @Override
    public CompletableFuture alarmDisarm(io.etcd.jetcd.maintenance.AlarmMember member) {
        checkArgument(member.getMemberId() != 0, "the member id can not be 0");
        checkArgument(member.getAlarmType() != io.etcd.jetcd.maintenance.AlarmType.NONE, "alarm type can not be NONE");

        AlarmRequest alarmRequest = AlarmRequest.newBuilder()
            .setAlarm(AlarmType.NOSPACE)
            .setAction(AlarmRequest.AlarmAction.DEACTIVATE)
            .setMemberID(member.getMemberId())
            .build();

        return completable(this.stub.alarm(alarmRequest), AlarmResponse::new);
    }

    @Override
    public CompletableFuture defragmentMember(String target) {
        return this.connectionManager().withNewChannel(
            target,
            VertxMaintenanceGrpc::newVertxStub,
            stub -> {
                return stub.defragment(DefragmentRequest.getDefaultInstance())
                    .map(DefragmentResponse::new)
                    .toCompletionStage().toCompletableFuture();
            });
    }

    @Override
    public CompletableFuture statusMember(String target) {
        return this.connectionManager().withNewChannel(
            target,
            VertxMaintenanceGrpc::newVertxStub,
            stub -> {
                return stub.status(StatusRequest.getDefaultInstance())
                    .map(StatusResponse::new)
                    .toCompletionStage().toCompletableFuture();
            });
    }

    @Override
    public CompletableFuture moveLeader(long transfereeID) {
        return completable(
            this.stub.moveLeader(MoveLeaderRequest.newBuilder().setTargetID(transfereeID).build()),
            MoveLeaderResponse::new);
    }

    @Override
    public CompletableFuture hashKV(String target, long rev) {
        return this.connectionManager().withNewChannel(
            target,
            VertxMaintenanceGrpc::newVertxStub,
            stub -> {
                return stub.hashKV(HashKVRequest.newBuilder().setRevision(rev).build())
                    .map(HashKVResponse::new)
                    .toCompletionStage().toCompletableFuture();
            });
    }

    @Override
    public CompletableFuture snapshot(OutputStream outputStream) {
        final CompletableFuture answer = new CompletableFuture<>();
        final AtomicLong bytes = new AtomicLong(0);

        this.stub.snapshotWithHandler(
            SnapshotRequest.getDefaultInstance(),
            r -> {
                try {
                    r.getBlob().writeTo(outputStream);
                    bytes.addAndGet(r.getBlob().size());
                } catch (IOException e) {
                    answer.completeExceptionally(toEtcdException(e));
                }
            },
            event -> {
                answer.complete(bytes.get());
            },
            e -> {
                answer.completeExceptionally(toEtcdException(e));
            });

        return answer;
    }

    @Override
    public void snapshot(StreamObserver observer) {

        this.stub.snapshotWithHandler(
            SnapshotRequest.getDefaultInstance(),
            r -> observer.onNext(new io.etcd.jetcd.maintenance.SnapshotResponse(r)),
            event -> observer.onCompleted(),
            e -> observer.onError(toEtcdException(e)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy