Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.curator.framework.recipes.queue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.apache.curator.utils.CloseableUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.api.CuratorEventType;
import org.apache.curator.framework.imps.CuratorFrameworkState;
import org.apache.curator.framework.listen.ListenerContainer;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.utils.ZKPaths;
import qcloud.storage.ranger.thirdparty.org.apache.zookeeper.CreateMode;
import qcloud.storage.ranger.thirdparty.org.apache.zookeeper.KeeperException;
import qcloud.storage.ranger.thirdparty.org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.curator.utils.PathUtils;
/**
*
An implementation of the Distributed Queue ZK recipe. Items put into the queue
* are guaranteed to be ordered (by means of ZK's PERSISTENT_SEQUENTIAL node).
*
*
* Guarantees:
*
*
If a single consumer takes items out of the queue, they will be ordered FIFO. i.e. if ordering is important,
* use a {@link LeaderSelector} to nominate a single consumer.
*
Unless a {@link QueueBuilder#lockPath(String)} is used, there is only guaranteed processing of each message to the point of receipt by a given instance.
*
If an instance receives an item from the queue but dies while processing it, the item will be lost. If you need message recoverability, use
* a {@link QueueBuilder#lockPath(String)}
*
*/
public class DistributedQueue implements QueueBase
{
private final Logger log = LoggerFactory.getLogger(getClass());
private final CuratorFramework client;
private final QueueSerializer serializer;
private final String queuePath;
private final Executor executor;
private final ExecutorService service;
private final AtomicReference state = new AtomicReference(State.LATENT);
private final QueueConsumer consumer;
private final int minItemsBeforeRefresh;
private final boolean refreshOnWatch;
private final boolean isProducerOnly;
private final String lockPath;
private final AtomicReference errorMode = new AtomicReference(ErrorMode.REQUEUE);
private final ListenerContainer> putListenerContainer = new ListenerContainer>();
private final AtomicInteger lastChildCount = new AtomicInteger(0);
private final int maxItems;
private final int finalFlushMs;
private final boolean putInBackground;
private final ChildrenCache childrenCache;
private final AtomicInteger putCount = new AtomicInteger(0);
private enum State
{
LATENT,
STARTED,
STOPPED
}
@VisibleForTesting
protected enum ProcessType
{
NORMAL,
REMOVE
}
private static final String QUEUE_ITEM_NAME = "queue-";
DistributedQueue
(
CuratorFramework client,
QueueConsumer consumer,
QueueSerializer serializer,
String queuePath,
ThreadFactory threadFactory,
Executor executor,
int minItemsBeforeRefresh,
boolean refreshOnWatch,
String lockPath,
int maxItems,
boolean putInBackground,
int finalFlushMs
)
{
Preconditions.checkNotNull(client, "client cannot be null");
Preconditions.checkNotNull(serializer, "serializer cannot be null");
Preconditions.checkNotNull(threadFactory, "threadFactory cannot be null");
Preconditions.checkNotNull(executor, "executor cannot be null");
Preconditions.checkArgument(maxItems > 0, "maxItems must be a positive number");
isProducerOnly = (consumer == null);
this.lockPath = (lockPath == null) ? null : PathUtils.validatePath(lockPath);
this.putInBackground = putInBackground;
this.consumer = consumer;
this.minItemsBeforeRefresh = minItemsBeforeRefresh;
this.refreshOnWatch = refreshOnWatch;
this.client = client;
this.serializer = serializer;
this.queuePath = PathUtils.validatePath(queuePath);
this.executor = executor;
this.maxItems = maxItems;
this.finalFlushMs = finalFlushMs;
service = Executors.newFixedThreadPool(2, threadFactory);
childrenCache = new ChildrenCache(client, queuePath);
if ( (maxItems != QueueBuilder.NOT_SET) && putInBackground )
{
log.warn("Bounded queues should set putInBackground(false) in the builder. Putting in the background will result in spotty maxItem consistency.");
}
}
/**
* Start the queue. No other methods work until this is called
*
* @throws Exception startup errors
*/
@Override
public void start() throws Exception
{
if ( !state.compareAndSet(State.LATENT, State.STARTED) )
{
throw new IllegalStateException();
}
try
{
client.create().creatingParentsIfNeeded().forPath(queuePath);
}
catch ( KeeperException.NodeExistsException ignore )
{
// this is OK
}
if ( lockPath != null )
{
try
{
client.create().creatingParentsIfNeeded().forPath(lockPath);
}
catch ( KeeperException.NodeExistsException ignore )
{
// this is OK
}
}
if ( !isProducerOnly || (maxItems != QueueBuilder.NOT_SET) )
{
childrenCache.start();
}
if ( !isProducerOnly )
{
service.submit
(
new Callable