zookeeper-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [zookeeper] eolivelli commented on issue #965: ZOOKEEPER-3409 Compile Java code with -Xdoclint
Date Mon, 03 Jun 2019 09:52:29 GMT
eolivelli commented on issue #965: ZOOKEEPER-3409 Compile Java code with -Xdoclint
URL: https://github.com/apache/zookeeper/pull/965#issuecomment-498191055
 
 
   ```
   /**
    * 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.zookeeper.server;
   
   import java.io.Flushable;
   import java.io.IOException;
   import java.util.ArrayDeque;
   import java.util.Objects;
   import java.util.Queue;
   import java.util.concurrent.BlockingQueue;
   import java.util.concurrent.LinkedBlockingQueue;
   import java.util.concurrent.Semaphore;
   import java.util.concurrent.ThreadLocalRandom;
   
   import org.slf4j.Logger;
   import org.slf4j.LoggerFactory;
   
   /**
    * This RequestProcessor logs requests to disk. It batches the requests to do
    * the io efficiently. The request is not passed to the next RequestProcessor
    * until its log has been synced to disk.
    *
    * SyncRequestProcessor is used in 3 different cases
    * 1. Leader - Sync request to disk and forward it to AckRequestProcessor which
    *             send ack back to itself.
    * 2. Follower - Sync request to disk and forward request to
    *             SendAckRequestProcessor which send the packets to leader.
    *             SendAckRequestProcessor is flushable which allow us to force
    *             push packets to leader.
    * 3. Observer - Sync committed request to disk (received as INFORM packet).
    *             It never send ack back to the leader, so the nextProcessor will
    *             be null. This change the semantic of txnlog on the observer
    *             since it only contains committed txns.
    */
   public class SyncRequestProcessor extends ZooKeeperCriticalThread implements
           RequestProcessor {
   
       private static final Logger LOG = LoggerFactory.getLogger(SyncRequestProcessor.class);
   
       private static final int FLUSH_SIZE = 1000;
   
       private static final Request REQUEST_OF_DEATH = Request.requestOfDeath;
   
       /** The number of log entries to log before starting a snapshot */
       private static int snapCount = ZooKeeperServer.getSnapCount();
   
       /**
        * The total size of log entries before starting a snapshot
        */
       private static long snapSizeInBytes = ZooKeeperServer.getSnapSizeInBytes();
   
       /**
        * Random numbers used to vary snapshot timing
        */
       private int randRoll;
       private long randSize;
   
       private final BlockingQueue<Request> queuedRequests =
           new LinkedBlockingQueue<Request>();
   
       private final Semaphore snapThreadMutex = new Semaphore(1);
   
       private final ZooKeeperServer zks;
   
       private final RequestProcessor nextProcessor;
   
       /**
        * Transactions that have been written and are waiting to be flushed to
        * disk. Basically this is the list of SyncItems whose callbacks will be
        * invoked after flush returns successfully.
        */
       private final Queue<Request> toFlush = new ArrayDeque<>(FLUSH_SIZE);
   
       public SyncRequestProcessor(ZooKeeperServer zks,
               RequestProcessor nextProcessor) {
           super("SyncThread:" + zks.getServerId(), zks
                   .getZooKeeperServerListener());
           this.zks = zks;
           this.nextProcessor = nextProcessor;
       }
   
       /**
        * used by tests to check for changing
        * snapcounts
        * @param count
        */
       public static void setSnapCount(int count) {
           snapCount = count;
       }
   
       /**
        * used by tests to get the snapcount
        * @return the snapcount
        */
       public static int getSnapCount() {
           return snapCount;
       }
   
       /**
        * used by tests to check for changing
        * snapcounts
        * @param size
        */
       public static void setSnapSizeInBytes(long size) {
           snapSizeInBytes = size;
       }
   
       private boolean shouldSnapshot() {
           int logCount = zks.getZKDatabase().getTxnCount();
           long logSize = zks.getZKDatabase().getTxnSize();
           return (logCount > (snapCount / 2 + randRoll)) ||
                   (snapSizeInBytes > 0 && logSize > (snapSizeInBytes / 2 +
randSize));
       }
   
       private void resetSnapshotStats() {
           randRoll = ThreadLocalRandom.current().nextInt(snapCount/2);
           randSize = Math.abs(ThreadLocalRandom.current().nextLong() % (snapSizeInBytes/2));
       }
   
       @Override
       public void run() {
           try {
               // we do this in an attempt to ensure that not all of the servers
               // in the ensemble take a snapshot at the same time
               resetSnapshotStats();
               while (true) {
                   Request si = queuedRequests.poll();
                   if (si == null) {
                       flush();
                       si = queuedRequests.take();
                   }
     
                   if (si == REQUEST_OF_DEATH) {
                       break;
                   }
   
                   // track the number of records written to the log
                   if (zks.getZKDatabase().append(si)) {
                       if (shouldSnapshot()) {
                           resetSnapshotStats();
                           // roll the log
                           zks.getZKDatabase().rollLog();
                           // take a snapshot
                           if (!snapThreadMutex.tryAcquire()) {
                               LOG.warn("Too busy to snap, skipping");
                           } else {
                               new ZooKeeperThread("Snapshot Thread") {
                                   public void run() {
                                       try {
                                           zks.takeSnapshot();
                                       } catch (Exception e) {
                                           LOG.warn("Unexpected exception", e);
                                       } finally {
                                         snapThreadMutex.release();
                                       }
                                   }
                               }.start();
                           }
                       }
                   } else if (toFlush.isEmpty()) {
                       // optimization for read heavy workloads
                       // iff this is a read, and there are no pending
                       // flushes (writes), then just pass this to the next
                       // processor
                       if (nextProcessor != null) {
                           nextProcessor.processRequest(si);
                           if (nextProcessor instanceof Flushable) {
                               ((Flushable)nextProcessor).flush();
                           }
                       }
                       continue;
                   }
                   toFlush.add(si);
                   if (toFlush.size() == FLUSH_SIZE) {
                       flush();
                   }
               }
           } catch (Throwable t) {
               handleException(this.getName(), t);
           }
           LOG.info("SyncRequestProcessor exited!");
       }
   
       private void flush() throws IOException, RequestProcessorException {
         if (this.toFlush.isEmpty()) {
             return;
         }
   
         zks.getZKDatabase().commit();
   
         if (this.nextProcessor == null) {
           this.toFlush.clear();
         } else {
             while (!this.toFlush.isEmpty()) {
                 final Request i = this.toFlush.remove();
                 this.nextProcessor.processRequest(i);
             }
             if (this.nextProcessor instanceof Flushable) {
                 ((Flushable)this.nextProcessor).flush();
             } 
         }
       }
   
       public void shutdown() {
           LOG.info("Shutting down");
           queuedRequests.add(REQUEST_OF_DEATH);
           try {
               this.join();
               this.flush();
           } catch (InterruptedException e) {
               LOG.warn("Interrupted while wating for " + this + " to finish");
               Thread.currentThread().interrupt();
           } catch (IOException e) {
               LOG.warn("Got IO exception during shutdown");
           } catch (RequestProcessorException e) {
               LOG.warn("Got request processor exception during shutdown");
           }
           if (nextProcessor != null) {
               nextProcessor.shutdown();
           }
       }
   
       public void processRequest(final Request request) {
           Objects.requireNonNull(request, "Request cannot be null");
           queuedRequests.add(request);
       }
   
   }
   
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message