• Hadoop源码分析10: IPC流程(5) Atomic


    1.Class类的running 

    用于主线程的Client  和若干 ClientConnection 线程之间 共享Client的running变量

    public classClient {
      public AtomicBoolean running = newAtomicBoolean(true); // if client runs

     public void stop() {
      ...............
        if(!running.compareAndSet(true, false)) {
         return;
        }
     .................
     }

      private ClientConnectiongetConnection(ClientConnectionId remoteId,
                                  ClientCallcall)
                                  throwsIOException, InterruptedException {
        if(!running.get()) {
          //the client is stopped
          thrownew IOException("The client is stopped");
        }

    }


    public class ClientConnection extends Thread {
        private synchronizedboolean waitForWork() {
          if(calls.isEmpty() && !shouldCloseConnection.get() && client.running.get()) {
           long timeout = maxIdleTime-
               (System.currentTimeMillis()-lastActivity.get());
           if (timeout>0) {
             try {
              wait(timeout);
             } catch (InterruptedExceptione) {}
           }
         }
        if (!calls.isEmpty()&& !shouldCloseConnection.get() && client.running.get()) {
           return true;
          } 
        。。。。。。。。。。。。。。
    }

    public class ClientConnectionPingInputStream extendsFilterInputStream {

        private voidhandleTimeout(SocketTimeoutException e) throws IOException{
          if(clientConnection.shouldCloseConnection.get() ||!clientConnection.client.running.get()  ||clientConnection.rpcTimeout > 0) {
           throw e;
          }else {
           sendPing();
         }
        }
    。。。。。。
    }

    2. ClientConnectionlastActivity

    用于若干ClientConnection线程之间共享lastActivity

    public class ClientConnection extends Thread {

        private AtomicLonglastActivity = new AtomicLong();// last I/O activity time
        
       
        private voidtouch() {
         lastActivity.set(System.currentTimeMillis());
        }
     
       private synchronizedboolean waitForWork() {
          if(calls.isEmpty() && !shouldCloseConnection.get() && client.running.get()) {
           long timeout = maxIdleTime-
               (System.currentTimeMillis()-lastActivity.get());
           if (timeout>0) {
             try {
              wait(timeout);
             } catch (InterruptedExceptione) {}
           }
         }
        }
    }

    3. ClientConnection shouldCloseConnection 

    用于若干ClientConnection 线程之间共享shouldCloseConnection 

    public class ClientConnection extends Thread {

        AtomicBooleanshouldCloseConnection = new AtomicBoolean(); 
       // indicate if theconnection is closed

        public synchronizedboolean addCall(ClientCall call) {
          if(shouldCloseConnection.get())
           return false;
         calls.put(call.id, call);
         notify();
         return true;
        }

       public synchronized voidsetupIOstreams() throws InterruptedException {
          if(socket != null || shouldCloseConnection.get()) {
           return;
         }
        .........
       }

        private synchronizedboolean waitForWork() {
          if(calls.isEmpty() && !shouldCloseConnection.get() && client.running.get()) {
           long timeout = maxIdleTime-
               (System.currentTimeMillis()-lastActivity.get());
           if (timeout>0) {
             try {
              wait(timeout);
             } catch (InterruptedExceptione) {}
           }
         }
         
          if(!calls.isEmpty() && !shouldCloseConnection.get()&& client.running.get()) {
           return true;
          }else if (shouldCloseConnection.get()) {
           return false;
          }else if (calls.isEmpty()) { // idle connection closed orstopped
           markClosed(null);
           return false;
          }else { // get stopped but there are still pendingrequests 
           markClosed((IOException)newIOException().initCause(
               newInterruptedException()));
           return false;
         }
        }

       public voidsendParam(ClientCall call) {
          if(shouldCloseConnection.get()) {
           return;
         }
       }

        private voidreceiveResponse() {
          if(shouldCloseConnection.get()) {
           return;
         }
         touch();
       }
       
        private synchronizedvoid markClosed(IOException e) {
          if(shouldCloseConnection.compareAndSet(false, true)) {
           closeException = e;
           notifyAll();
         }
        }
        
       
        private synchronizedvoid close() {
          if(!shouldCloseConnection.get()) {
            return;
         }
        }
    }


    publicclass ClientConnectionPingInputStream extendsFilterInputStream {

        privatevoid handleTimeout(SocketTimeoutExceptione) throws IOException {
          if(clientConnection.shouldCloseConnection.get() ||!clientConnection.client.running.get()  ||clientConnection.rpcTimeout > 0) {
           throw e;
          }else {
           sendPing();
         }
        }
    。。。。。。
    }
  • 相关阅读:
    将内容重定向到剪切板(clip.exe)
    加速数组操作(Array)
    错误信息输出,重定向到文件
    格式化数字字符串
    PowerShell常用的.Net 、COM对象(New-Object、Assembly)、加载程序集
    计算文件夹大小、拷贝文件显示进度
    草稿-Hyper-V
    右下角显示提示窗口(New-Object,COM)
    《TCP/IP详解卷一:协议》数据链路层(一)
    tcpdump抓包命令
  • 原文地址:https://www.cnblogs.com/leeeee/p/7276527.html
Copyright © 2020-2023  润新知