@Override public Descriptor merge(Descriptor o) { WorkQueueDescriptor other = (WorkQueueDescriptor) o; WorkQueueDescriptor merged = new WorkQueueDescriptor(); merged.id = id; merged.name = other.name != null ? other.name : name; merged.queuing = other.queuing != null ? other.queuing : queuing; merged.capacity = other.capacity != null ? other.capacity : capacity; merged.processing = other.processing != null ? other.processing : processing; merged.maxThreads = other.maxThreads != null ? other.maxThreads : maxThreads; merged.categories = new HashSet<>(categories); merged.categories.addAll(other.categories); return merged; }
@Override public String toString() { StringBuilder buf = new StringBuilder(getClass().getSimpleName()); buf.append("(id="); buf.append(id); buf.append(" categories="); buf.append(categories); buf.append(" queuing="); buf.append(isQueuingEnabled()); buf.append(" processing="); buf.append(isProcessingEnabled()); buf.append(" maxThreads="); buf.append(getMaxThreads()); buf.append(" capacity="); buf.append(getCapacity()); buf.append(")"); return buf.toString(); } }
protected void index() { List<WorkQueueDescriptor> descriptors = getDescriptors(QUEUES_EP); descriptors.forEach(d -> d.categories.forEach(c -> { categoryToQueueId.computeIfPresent(c, (k, v) -> { if (!v.equals(d.getId())) { log.error("Work category '{}' cannot be assigned to work queue '{}'" + " because it is already assigned to work queue '{}'", c, d.getId(), v); } return v; }); categoryToQueueId.putIfAbsent(c, d.getId()); })); }
@Override void activateQueue(WorkQueueDescriptor config) { // queue processing is activated only from component listener afterStart if (WorkQueueDescriptor.ALL_QUEUES.equals(config.id)) { throw new IllegalArgumentException("cannot activate all queues"); } log.info("Activated queue " + config.id + " " + config.toString()); if (config.isProcessingEnabled()) { activateQueueMetrics(config.id); } }
protected void initTopology() { // create a single topology with one root per work pool Topology.Builder builder = Topology.builder(); List<WorkQueueDescriptor> descriptors = getDescriptors(QUEUES_EP); descriptors.stream().filter(WorkQueueDescriptor::isProcessingEnabled).forEach(d -> builder.addComputation( () -> new WorkComputation(d.getId()), Collections.singletonList("i1:" + d.getId()))); topology = builder.build(); settings = new Settings(DEFAULT_CONCURRENCY, getPartitions(DEFAULT_CONCURRENCY), getCodec()); descriptors.forEach(item -> settings.setConcurrency(item.getId(), item.getMaxThreads())); descriptors.forEach(item -> settings.setPartitions(item.getId(), getPartitions(item.getMaxThreads()))); }
@Override public void registerContribution(Object contribution, String xp, ComponentInstance component) { if (QUEUES_EP.equals(xp)) { WorkQueueDescriptor descriptor = (WorkQueueDescriptor) contribution; if (ALL_QUEUES.equals(descriptor.getId())) { Boolean processing = descriptor.processing; if (processing == null) { log.error("Ignoring work queue descriptor {} with no processing/queuing", ALL_QUEUES); return; } log.info("Setting on all work queues:{}", () -> " processing=" + processing + (queuing == null ? "" : " queuing=" + queuing)); // activate/deactivate processing on all queues getDescriptors(QUEUES_EP).forEach(d -> { WorkQueueDescriptor wqd = new WorkQueueDescriptor(); wqd.id = d.getId(); wqd.processing = processing; register(QUEUES_EP, wqd); }); } else { register(QUEUES_EP, descriptor); } } else { super.registerContribution(contribution, xp, component); } }
@Override void deactivateQueue(WorkQueueDescriptor config) { // queue processing is deactivated only on shutdown if (WorkQueueDescriptor.ALL_QUEUES.equals(config.id)) { throw new IllegalArgumentException("cannot deactivate all queues"); } if (config.isProcessingEnabled()) { deactivateQueueMetrics(config.id); } log.info("Deactivated work queue not supported: " + config.id); }
@Override public boolean isQueuingEnabled(String queueId) { WorkQueueDescriptor wqd = getWorkQueueDescriptor(queueId); return wqd != null && wqd.isQueuingEnabled(); }
@Override public MemoryBlockingQueue init(WorkQueueDescriptor config) { int capacity = config.getCapacity(); if (capacity <= 0) { capacity = -1; // unbounded } MemoryBlockingQueue queue = new MemoryBlockingQueue(config.id, this, capacity); allQueued.put(queue.queueId, queue); return queue; }
void initializeQueue(WorkQueueDescriptor config) { if (ALL_QUEUES.equals(config.id)) { throw new IllegalArgumentException("cannot initialize all queues"); } if (queuing.getQueue(config.id) != null) { throw new IllegalStateException("work queue " + config.id + " is already initialized"); } if (executors.containsKey(config.id)) { throw new IllegalStateException("work queue " + config.id + " already have an executor"); } NuxeoBlockingQueue queue = queuing.init(config); ThreadFactory threadFactory = new NamedThreadFactory(THREAD_PREFIX + config.id + "-"); int maxPoolSize = config.getMaxThreads(); WorkThreadPoolExecutor executor = new WorkThreadPoolExecutor(maxPoolSize, maxPoolSize, 0, TimeUnit.SECONDS, queue, threadFactory); // prestart all core threads so that direct additions to the queue // (from another Nuxeo instance) can be seen executor.prestartAllCoreThreads(); executors.put(config.id, executor); log.info("Initialized work queue {}, {}", config.id, config); }
void activateQueue(WorkQueueDescriptor config) { if (ALL_QUEUES.equals(config.id)) { throw new IllegalArgumentException("cannot activate all queues"); } queuing.setActive(config.id, config.isProcessingEnabled()); log.info("Activated work queue {}, {}", config.id, config); // Enable metrics if (config.isProcessingEnabled()) { activateQueueMetrics(config.id); } }
@Override public boolean isProcessingEnabled(String queueId) { WorkQueueDescriptor wqd = getWorkQueueDescriptor(queueId); return wqd != null && wqd.isProcessingEnabled(); }
void deactivateQueue(WorkQueueDescriptor config) { if (ALL_QUEUES.equals(config.id)) { throw new IllegalArgumentException("cannot deactivate all queues"); } // Disable metrics if (config.isProcessingEnabled()) { deactivateQueueMetrics(config.id); } queuing.setActive(config.id, false); log.info("Deactivated work queue {}", config.id); }