TaskFuture.java 5.0 KB
Newer Older
T
Tboy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.dolphinscheduler.server.master.future;


import org.apache.dolphinscheduler.remote.command.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
31
import java.util.concurrent.atomic.AtomicReference;
T
Tboy 已提交
32

Q
qiaozhanwei 已提交
33
/**
34
 *  task future
Q
qiaozhanwei 已提交
35
 */
T
Tboy 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
public class TaskFuture {

    private final static Logger LOGGER = LoggerFactory.getLogger(TaskFuture.class);

    private final static ConcurrentHashMap<Long,TaskFuture> FUTURE_TABLE = new ConcurrentHashMap<>(256);

    /**
     *  request unique identification
     */
    private final long opaque;

    /**
     *  timeout
     */
    private final long timeoutMillis;

    private final CountDownLatch latch = new CountDownLatch(1);

    private final long beginTimestamp = System.currentTimeMillis();

    /**
     *  response command
     */
59
    private  AtomicReference<Command> responseCommandReference = new AtomicReference<>();
T
Tboy 已提交
60 61 62

    private volatile boolean sendOk = true;

63
    private  AtomicReference<Throwable> causeReference;
T
Tboy 已提交
64 65 66 67 68 69 70 71

    public TaskFuture(long opaque, long timeoutMillis) {
        this.opaque = opaque;
        this.timeoutMillis = timeoutMillis;
        FUTURE_TABLE.put(opaque, this);
    }

    /**
Q
qiaozhanwei 已提交
72
     * wait for response
T
Tboy 已提交
73
     * @return command
74
     * @throws InterruptedException if error throws InterruptedException
T
Tboy 已提交
75 76 77
     */
    public Command waitResponse() throws InterruptedException {
        this.latch.await(timeoutMillis, TimeUnit.MILLISECONDS);
78
        return this.responseCommandReference.get();
T
Tboy 已提交
79 80 81 82 83 84 85 86
    }

    /**
     *  put response
     *
     * @param responseCommand responseCommand
     */
    public void putResponse(final Command responseCommand) {
87
        responseCommandReference.set(responseCommand);
T
Tboy 已提交
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
        this.latch.countDown();
        FUTURE_TABLE.remove(opaque);
    }

    /**
     *  whether timeout
     * @return timeout
     */
    public boolean isTimeout() {
        long diff = System.currentTimeMillis() - this.beginTimestamp;
        return diff > this.timeoutMillis;
    }

    public static void notify(final Command responseCommand){
        TaskFuture taskFuture = FUTURE_TABLE.remove(responseCommand.getOpaque());
        if(taskFuture != null){
            taskFuture.putResponse(responseCommand);
        }
    }


    public boolean isSendOK() {
        return sendOk;
    }

    public void setSendOk(boolean sendOk) {
        this.sendOk = sendOk;
    }

    public void setCause(Throwable cause) {
118
        causeReference.set(cause);
T
Tboy 已提交
119 120 121
    }

    public Throwable getCause() {
122
        return causeReference.get();
T
Tboy 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    }

    public long getOpaque() {
        return opaque;
    }

    public long getTimeoutMillis() {
        return timeoutMillis;
    }

    public long getBeginTimestamp() {
        return beginTimestamp;
    }

    public Command getResponseCommand() {
138
        return responseCommandReference.get();
T
Tboy 已提交
139 140 141
    }

    public void setResponseCommand(Command responseCommand) {
142
        responseCommandReference.set(responseCommand);
T
Tboy 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    }


    /**
     * scan future table
     */
    public static void scanFutureTable(){
        final List<TaskFuture> futureList = new LinkedList<>();
        Iterator<Map.Entry<Long, TaskFuture>> it = FUTURE_TABLE.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Long, TaskFuture> next = it.next();
            TaskFuture future = next.getValue();
            if ((future.getBeginTimestamp() + future.getTimeoutMillis() + 1000) <= System.currentTimeMillis()) {
                futureList.add(future);
                it.remove();
                LOGGER.warn("remove timeout request : {}", future);
            }
        }
    }
Q
qiaozhanwei 已提交
162 163 164 165 166 167 168 169

    @Override
    public String toString() {
        return "TaskFuture{" +
                "opaque=" + opaque +
                ", timeoutMillis=" + timeoutMillis +
                ", latch=" + latch +
                ", beginTimestamp=" + beginTimestamp +
170
                ", responseCommand=" + responseCommandReference.get() +
Q
qiaozhanwei 已提交
171
                ", sendOk=" + sendOk +
172
                ", cause=" + causeReference.get() +
Q
qiaozhanwei 已提交
173 174
                '}';
    }
T
Tboy 已提交
175
}