|
19 | 19 |
|
20 | 20 | package com.imageworks.spcue.dispatcher;
|
21 | 21 |
|
22 |
| -import java.util.concurrent.LinkedBlockingQueue; |
23 |
| -import java.util.concurrent.ThreadPoolExecutor; |
24 |
| -import java.util.concurrent.TimeUnit; |
25 |
| -import java.util.concurrent.atomic.AtomicBoolean; |
26 |
| - |
27 | 22 | import com.google.common.cache.Cache;
|
28 | 23 | import com.google.common.cache.CacheBuilder;
|
29 |
| -import com.imageworks.spcue.dispatcher.commands.DispatchBookHost; |
30 |
| -import com.imageworks.spcue.util.CueUtil; |
| 24 | +import com.imageworks.spcue.dispatcher.commands.KeyRunnable; |
31 | 25 |
|
32 | 26 | import org.apache.log4j.Logger;
|
33 | 27 | import org.springframework.beans.factory.annotation.Autowired;
|
34 | 28 | import org.springframework.core.env.Environment;
|
35 | 29 |
|
36 |
| -public class BookingQueue extends ThreadPoolExecutor { |
37 |
| - |
38 |
| - private static final Logger logger = Logger.getLogger(BookingQueue.class); |
39 |
| - |
40 |
| - private static final int THREADS_KEEP_ALIVE_SECONDS = 10; |
| 30 | +public class BookingQueue { |
41 | 31 |
|
42 |
| - private int queueCapacity; |
43 |
| - private int baseSleepTimeMillis = 400; |
44 |
| - private AtomicBoolean isShutdown = new AtomicBoolean(false); |
| 32 | + private final int healthThreshold; |
| 33 | + private final int minUnhealthyPeriodMin; |
| 34 | + private final int queueCapacity; |
| 35 | + private final int corePoolSize; |
| 36 | + private final int maxPoolSize; |
| 37 | + private static final int BASE_SLEEP_TIME_MILLIS = 300; |
45 | 38 |
|
46 |
| - private QueueRejectCounter rejectCounter = new QueueRejectCounter(); |
| 39 | + private static final Logger logger = Logger.getLogger("HEALTH"); |
| 40 | + private HealthyThreadPool healthyThreadPool; |
47 | 41 |
|
48 |
| - private Cache<String, DispatchBookHost> bookingCache = CacheBuilder.newBuilder() |
49 |
| - .expireAfterWrite(3, TimeUnit.MINUTES) |
50 |
| - // Invalidate entries that got executed by the threadpool and lost their reference |
51 |
| - .weakValues() |
52 |
| - .build(); |
53 |
| - |
54 |
| - private BookingQueue(int corePoolSize, int maxPoolSize, int queueCapacity, int sleepTimeMs) { |
55 |
| - super(corePoolSize, maxPoolSize, THREADS_KEEP_ALIVE_SECONDS, |
56 |
| - TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(queueCapacity)); |
| 42 | + public BookingQueue(int healthThreshold, int minUnhealthyPeriodMin, int queueCapacity, |
| 43 | + int corePoolSize, int maxPoolSize) { |
| 44 | + this.healthThreshold = healthThreshold; |
| 45 | + this.minUnhealthyPeriodMin = minUnhealthyPeriodMin; |
57 | 46 | this.queueCapacity = queueCapacity;
|
58 |
| - this.baseSleepTimeMillis = sleepTimeMs; |
59 |
| - this.setRejectedExecutionHandler(rejectCounter); |
60 |
| - logger.info("BookingQueue" + |
61 |
| - " core:" + getCorePoolSize() + |
62 |
| - " max:" + getMaximumPoolSize() + |
63 |
| - " capacity:" + queueCapacity + |
64 |
| - " sleepTimeMs:" + sleepTimeMs); |
| 47 | + this.corePoolSize = corePoolSize; |
| 48 | + this.maxPoolSize = maxPoolSize; |
| 49 | + initThreadPool(); |
65 | 50 | }
|
66 | 51 |
|
67 |
| - @Autowired |
68 |
| - public BookingQueue(Environment env, String propertyKeyPrefix, int sleepTimeMs) { |
69 |
| - this(CueUtil.getIntProperty(env, propertyKeyPrefix, "core_pool_size"), |
70 |
| - CueUtil.getIntProperty(env, propertyKeyPrefix, "max_pool_size"), |
71 |
| - CueUtil.getIntProperty(env, propertyKeyPrefix, "queue_capacity"), |
72 |
| - sleepTimeMs); |
| 52 | + public void initThreadPool() { |
| 53 | + healthyThreadPool = new HealthyThreadPool( |
| 54 | + "BookingQueue", |
| 55 | + healthThreshold, |
| 56 | + minUnhealthyPeriodMin, |
| 57 | + queueCapacity, |
| 58 | + corePoolSize, |
| 59 | + maxPoolSize, |
| 60 | + BASE_SLEEP_TIME_MILLIS); |
73 | 61 | }
|
74 | 62 |
|
75 |
| - public void execute(DispatchBookHost r) { |
76 |
| - if (isShutdown.get()) { |
77 |
| - return; |
78 |
| - } |
79 |
| - if (bookingCache.getIfPresent(r.getKey()) == null){ |
80 |
| - bookingCache.put(r.getKey(), r); |
81 |
| - super.execute(r); |
| 63 | + public boolean isHealthy() { |
| 64 | + try { |
| 65 | + if (!healthyThreadPool.isHealthyOrShutdown()) { |
| 66 | + logger.warn("BookingQueue: Unhealthy queue terminated, starting a new one"); |
| 67 | + initThreadPool(); |
| 68 | + } |
| 69 | + } catch (InterruptedException e) { |
| 70 | + // TODO: evaluate crashing the whole springbook context here |
| 71 | + // to force a container restart cycle |
| 72 | + logger.error("Failed to restart BookingThreadPool", e); |
| 73 | + return false; |
82 | 74 | }
|
| 75 | + |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + public void execute(KeyRunnable r) { |
| 80 | + healthyThreadPool.execute(r); |
83 | 81 | }
|
84 | 82 |
|
85 | 83 | public long getRejectedTaskCount() {
|
86 |
| - return rejectCounter.getRejectCount(); |
| 84 | + return healthyThreadPool.getRejectedTaskCount(); |
87 | 85 | }
|
88 | 86 |
|
89 | 87 | public int getQueueCapacity() {
|
90 | 88 | return queueCapacity;
|
91 | 89 | }
|
92 | 90 |
|
93 | 91 | public void shutdown() {
|
94 |
| - if (!isShutdown.getAndSet(true)) { |
95 |
| - logger.info("clearing out booking queue: " + this.getQueue().size()); |
96 |
| - this.getQueue().clear(); |
97 |
| - } |
| 92 | + healthyThreadPool.shutdown(); |
| 93 | + } |
98 | 94 |
|
| 95 | + public int getSize() { |
| 96 | + return healthyThreadPool.getQueue().size(); |
99 | 97 | }
|
100 | 98 |
|
101 |
| - /** |
102 |
| - * Lowers the sleep time as the queue grows. |
103 |
| - * |
104 |
| - * @return |
105 |
| - */ |
106 |
| - public int sleepTime() { |
107 |
| - if (!isShutdown.get()) { |
108 |
| - int sleep = (int) (baseSleepTimeMillis - (((this.getQueue().size () / |
109 |
| - (float) queueCapacity) * baseSleepTimeMillis)) * 2); |
110 |
| - if (sleep < 0) { |
111 |
| - sleep = 0; |
112 |
| - } |
113 |
| - return sleep; |
114 |
| - } else { |
115 |
| - return 0; |
116 |
| - } |
| 99 | + public int getRemainingCapacity() { |
| 100 | + return healthyThreadPool.getQueue().remainingCapacity(); |
117 | 101 | }
|
118 | 102 |
|
119 |
| - protected void beforeExecute(Thread t, Runnable r) { |
120 |
| - super.beforeExecute(t, r); |
121 |
| - if (isShutdown()) { |
122 |
| - this.remove(r); |
123 |
| - } else { |
124 |
| - try { |
125 |
| - Thread.sleep(sleepTime()); |
126 |
| - } catch (InterruptedException e) { |
127 |
| - logger.info("booking queue was interrupted."); |
128 |
| - Thread.currentThread().interrupt(); |
129 |
| - } |
130 |
| - } |
| 103 | + public int getActiveCount() { |
| 104 | + return healthyThreadPool.getActiveCount(); |
131 | 105 | }
|
132 | 106 |
|
133 |
| - protected void afterExecute(Runnable r, Throwable t) { |
134 |
| - super.afterExecute(r, t); |
| 107 | + public long getCompletedTaskCount() { |
| 108 | + return healthyThreadPool.getCompletedTaskCount(); |
| 109 | + } |
135 | 110 |
|
136 |
| - // Invalidate cache to avoid having to wait for GC to mark processed entries collectible |
137 |
| - DispatchBookHost h = (DispatchBookHost)r; |
138 |
| - bookingCache.invalidate(h.getKey()); |
| 111 | + public long getCorePoolSize() { |
| 112 | + return corePoolSize; |
| 113 | + } |
139 | 114 |
|
140 |
| - if (sleepTime() < 100) { |
141 |
| - logger.info("BookingQueue cleanup executed."); |
142 |
| - getQueue().clear(); |
143 |
| - } |
| 115 | + public long getMaximumPoolSize() { |
| 116 | + return maxPoolSize; |
144 | 117 | }
|
| 118 | + |
145 | 119 | }
|
146 | 120 |
|
0 commit comments