RemoteWorkerRef.java 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * 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.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

P
pengys5 已提交
19
package org.skywalking.apm.collector.stream.worker;
P
pengys5 已提交
20

P
pengys5 已提交
21
import io.grpc.stub.StreamObserver;
P
pengys5 已提交
22
import org.skywalking.apm.collector.client.grpc.GRPCClient;
P
pengys5 已提交
23
import org.skywalking.apm.collector.core.util.Const;
P
pengys5 已提交
24
import org.skywalking.apm.collector.remote.grpc.proto.Empty;
P
pengys5 已提交
25 26 27
import org.skywalking.apm.collector.remote.grpc.proto.RemoteCommonServiceGrpc;
import org.skywalking.apm.collector.remote.grpc.proto.RemoteData;
import org.skywalking.apm.collector.remote.grpc.proto.RemoteMessage;
P
pengys5 已提交
28 29
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
P
pengys5 已提交
30

P
pengys5 已提交
31 32 33
/**
 * @author pengys5
 */
34
public class RemoteWorkerRef extends WorkerRef {
P
pengys5 已提交
35

P
pengys5 已提交
36 37
    private final Logger logger = LoggerFactory.getLogger(RemoteWorkerRef.class);

P
pengys5 已提交
38
    private final Boolean acrossJVM;
P
pengys5 已提交
39 40
    private final RemoteCommonServiceGrpc.RemoteCommonServiceStub stub;
    private StreamObserver<RemoteMessage> streamObserver;
P
pengys5 已提交
41
    private final AbstractRemoteWorker remoteWorker;
P
pengys5 已提交
42
    private final String address;
P
pengys5 已提交
43 44 45 46 47 48

    public RemoteWorkerRef(Role role, AbstractRemoteWorker remoteWorker) {
        super(role);
        this.remoteWorker = remoteWorker;
        this.acrossJVM = false;
        this.stub = null;
P
pengys5 已提交
49
        this.address = Const.EMPTY_STRING;
P
pengys5 已提交
50
    }
P
pengys5 已提交
51

P
pengys5 已提交
52
    public RemoteWorkerRef(Role role, GRPCClient client) {
P
pengys5 已提交
53
        super(role);
P
pengys5 已提交
54 55
        this.remoteWorker = null;
        this.acrossJVM = true;
P
pengys5 已提交
56
        this.stub = RemoteCommonServiceGrpc.newStub(client.getChannel());
P
pengys5 已提交
57
        this.address = client.toString();
P
pengys5 已提交
58
        createStreamObserver();
P
pengys5 已提交
59 60 61 62
    }

    @Override
    public void tell(Object message) throws WorkerInvokeException {
P
pengys5 已提交
63
        if (acrossJVM) {
P
pengys5 已提交
64 65 66 67 68 69 70 71 72 73
            try {
                RemoteData remoteData = getRole().dataDefine().serialize(message);
                RemoteMessage.Builder builder = RemoteMessage.newBuilder();
                builder.setWorkerRole(getRole().roleName());
                builder.setRemoteData(remoteData);

                streamObserver.onNext(builder.build());
            } catch (Throwable e) {
                logger.error(e.getMessage(), e);
            }
P
pengys5 已提交
74 75 76 77 78 79 80
        } else {
            remoteWorker.allocateJob(message);
        }
    }

    public Boolean isAcrossJVM() {
        return acrossJVM;
P
pengys5 已提交
81
    }
P
pengys5 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

    private void createStreamObserver() {
        StreamStatus status = new StreamStatus(false);
        streamObserver = stub.call(new StreamObserver<Empty>() {
            @Override public void onNext(Empty empty) {
            }

            @Override public void onError(Throwable throwable) {
                logger.error(throwable.getMessage(), throwable);
            }

            @Override public void onCompleted() {
                status.finished();
            }
        });
    }

    class StreamStatus {
        private volatile boolean status;

        public StreamStatus(boolean status) {
            this.status = status;
        }

        public boolean isFinish() {
            return status;
        }

        public void finished() {
            this.status = true;
        }

        /**
         * @param maxTimeout max wait time, milliseconds.
         */
        public void wait4Finish(long maxTimeout) {
            long time = 0;
            while (!status) {
                if (time > maxTimeout) {
                    break;
                }
                try2Sleep(5);
                time += 5;
            }
        }

        /**
         * Try to sleep, and ignore the {@link InterruptedException}
         *
         * @param millis the length of time to sleep in milliseconds
         */
        private void try2Sleep(long millis) {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {

            }
        }
    }
P
pengys5 已提交
141 142 143 144 145 146 147

    @Override public String toString() {
        StringBuilder toString = new StringBuilder();
        toString.append("acrossJVM: ").append(acrossJVM);
        toString.append(", address: ").append(address);
        return toString.toString();
    }
P
pengys5 已提交
148
}