001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import java.util.List; 021import java.util.Map; 022import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 023import org.apache.yetus.audience.InterfaceAudience; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027/** 028 * A wrapper for a runnable for a group of actions for a single regionserver. 029 * <p> 030 * This can be used to build up the actions that should be taken and then 031 * </p> 032 * <p> 033 * This class exists to simulate using a ScheduledExecutorService with just a regular 034 * ExecutorService and Runnables. It is used for legacy reasons in the the client; this could only 035 * be removed if we change the expectations in HTable around the pool the client is able to pass in 036 * and even if we deprecate the current APIs would require keeping this class around for the interim 037 * to bridge between the legacy ExecutorServices and the scheduled pool. 038 * </p> 039 */ 040@InterfaceAudience.Private 041public class DelayingRunner implements Runnable { 042 private static final Logger LOG = LoggerFactory.getLogger(DelayingRunner.class); 043 044 private final Object sleepLock = new Object(); 045 private boolean triggerWake = false; 046 private long sleepTime; 047 private MultiAction actions = new MultiAction(); 048 private Runnable runnable; 049 050 public DelayingRunner(long sleepTime, Map.Entry<byte[], List<Action>> e) { 051 this.sleepTime = sleepTime; 052 add(e); 053 } 054 055 public void setRunner(Runnable runner) { 056 this.runnable = runner; 057 } 058 059 @Override 060 public void run() { 061 if (!sleep()) { 062 LOG.warn("Interrupted while sleeping for expected sleep time " + sleepTime + " ms"); 063 } 064 // TODO maybe we should consider switching to a listenableFuture for the actual callable and 065 // then handling the results/errors as callbacks. That way we can decrement outstanding tasks 066 // even if we get interrupted here, but for now, we still need to run so we decrement the 067 // outstanding tasks 068 this.runnable.run(); 069 } 070 071 /** 072 * Sleep for an expected amount of time. 073 * <p> 074 * This is nearly a copy of what the Sleeper does, but with the ability to know if you got 075 * interrupted while sleeping. 076 * </p> 077 * @return <tt>true</tt> if the sleep completely entirely successfully, but otherwise 078 * <tt>false</tt> if the sleep was interrupted. 079 */ 080 private boolean sleep() { 081 long now = EnvironmentEdgeManager.currentTime(); 082 long startTime = now; 083 long waitTime = sleepTime; 084 while (waitTime > 0) { 085 long woke = -1; 086 try { 087 synchronized (sleepLock) { 088 if (triggerWake) break; 089 sleepLock.wait(waitTime); 090 } 091 woke = EnvironmentEdgeManager.currentTime(); 092 } catch (InterruptedException iex) { 093 return false; 094 } 095 // Recalculate waitTime. 096 woke = (woke == -1) ? EnvironmentEdgeManager.currentTime() : woke; 097 waitTime = waitTime - (woke - startTime); 098 } 099 return true; 100 } 101 102 public void add(Map.Entry<byte[], List<Action>> e) { 103 actions.add(e.getKey(), e.getValue()); 104 } 105 106 public MultiAction getActions() { 107 return actions; 108 } 109 110 public long getSleepTime() { 111 return sleepTime; 112 } 113}