2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.lockingservice;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.apache.zookeeper.CreateMode;
27 import org.apache.zookeeper.KeeperException;
28 import org.apache.zookeeper.ZooDefs;
29 import org.apache.zookeeper.ZooKeeper;
30 import org.apache.zookeeper.data.ACL;
31 import org.apache.zookeeper.data.Stat;
32 import org.onap.music.eelf.logging.EELFLoggerDelegate;
33 import org.onap.music.lockingservice.ZooKeeperOperation;
34 import java.util.List;
35 import java.util.concurrent.atomic.AtomicBoolean;
38 * A base class for protocol implementations which provides a number of higher level helper methods
39 * for working with ZooKeeper along with retrying synchronous operations if the connection to
40 * ZooKeeper closes such as {@link #retryOperation(ZooKeeperOperation)}
43 class ProtocolSupport {
44 private EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(ProtocolSupport.class);
46 protected ZooKeeper zookeeper;
47 private AtomicBoolean closed = new AtomicBoolean(false);
48 private long retryDelay = 500L;
49 private int retryCount = 10;
50 private List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
52 // public ProtocolSupport(ZooKeeper zookeeper) {
53 // this.zookeeper = zookeeper;
57 * Closes this strategy and releases any ZooKeeper resources; but keeps the ZooKeeper instance
61 if (closed.compareAndSet(false, true)) {
67 * return zookeeper client instance
69 * @return zookeeper client instance
71 public ZooKeeper getZookeeper() {
76 * return the acl its using
80 public List<ACL> getAcl() {
87 * @param acl the acl to set to
89 public void setAcl(List<ACL> acl) {
94 * get the retry delay in milliseconds
96 * @return the retry delay
98 public long getRetryDelay() {
103 * Sets the time waited between retry delays
105 * @param retryDelay the retry delay
107 public void setRetryDelay(long retryDelay) {
108 this.retryDelay = retryDelay;
112 * Allow derived classes to perform some custom closing operations to release resources
114 protected void doClose() {}
118 * Perform the given operation, retrying if the connection fails
120 * @return object. it needs to be cast to the callee's expected return type.
121 * @param operation FILL IN
122 * @throws KeeperException FILL IN
123 * @throws InterruptedException FILL IN
125 protected Object retryOperation(ZooKeeperOperation operation)
126 throws KeeperException, InterruptedException {
127 KeeperException exception = null;
128 for (int i = 0; i < retryCount; i++) {
130 return operation.execute();
131 } catch (KeeperException.SessionExpiredException e) {
132 LOG.debug("Session expired for: " + zookeeper + " so reconnecting due to: " + e, e);
134 } catch (KeeperException.ConnectionLossException e) {
135 if (exception == null) {
138 LOG.debug("Attempt " + i + " failed with connection loss so "
139 + "attempting to reconnect: " + e, e);
147 * Ensures that the given path exists with no data, the current ACL and no flags
149 * @param path the lock path
151 protected void ensurePathExists(String path) {
152 ensureExists(path, null, acl, CreateMode.PERSISTENT);
156 * Ensures that the given path exists with the given data, ACL and flags
158 * @param path the lock path
159 * @param data the data
160 * @param acl list of ACLs applying to the path
161 * @param flags create mode flags
163 protected void ensureExists(final String path, final byte[] data, final List<ACL> acl,
164 final CreateMode flags) {
166 retryOperation(new ZooKeeperOperation() {
167 public boolean execute() throws KeeperException, InterruptedException {
168 Stat stat = zookeeper.exists(path, false);
172 zookeeper.create(path, data, acl, flags);
176 } catch (KeeperException e) {
177 LOG.error(EELFLoggerDelegate.errorLogger,"Caught: " + e, e);
178 } catch (InterruptedException e) {
179 LOG.error(EELFLoggerDelegate.errorLogger,"Caught: " + e, e);
184 * Returns true if this protocol has been closed
186 * @return true if this protocol is closed
188 protected boolean isClosed() {
193 * Performs a retry delay if this is not the first attempt
195 * @param attemptCount the number of the attempts performed so far
197 protected void retryDelay(int attemptCount) {
198 if (attemptCount > 0) {
200 Thread.sleep(attemptCount * retryDelay);
201 } catch (InterruptedException e) {
202 LOG.error(EELFLoggerDelegate.errorLogger,"Failed to sleep: " + e, e);