2  * ============LICENSE_START=======================================================
 
   3  * feature-distributed-locking
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 
   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.
 
  18  * ============LICENSE_END=========================================================
 
  20 package org.onap.policy.distributed.locking;
 
  22 import java.sql.Connection;
 
  23 import java.sql.DriverManager;
 
  24 import java.sql.PreparedStatement;
 
  25 import java.sql.SQLException;
 
  26 import java.util.UUID;
 
  27 import java.util.concurrent.Executors;
 
  28 import java.util.concurrent.Future;
 
  29 import java.util.concurrent.ScheduledExecutorService;
 
  30 import java.util.concurrent.TimeUnit;
 
  32 import org.onap.policy.common.utils.properties.exception.PropertyException;
 
  33 import org.onap.policy.drools.core.lock.LockRequestFuture;
 
  34 import org.onap.policy.drools.core.lock.PolicyResourceLockFeatureAPI;
 
  35 import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
 
  36 import org.onap.policy.drools.persistence.SystemPersistence;
 
  37 import org.onap.policy.drools.system.PolicyEngine;
 
  38 import org.slf4j.Logger;
 
  39 import org.slf4j.LoggerFactory;
 
  41 public class DistributedLockingFeature implements PolicyEngineFeatureAPI, PolicyResourceLockFeatureAPI {
 
  46         private static final Logger logger = LoggerFactory.getLogger(DistributedLockingFeature.class);
 
  49          * Properties Configuration Name
 
  51         public static final String CONFIGURATION_PROPERTIES_NAME = "feature-distributed-locking";
 
  54          * Properties for locking feature
 
  56         private DistributedLockingProperties lockProps;
 
  59          *ScheduledExecutorService for LockHeartbeat 
 
  61         private ScheduledExecutorService scheduledExecutorService;
 
  66         private static final UUID uuid = UUID.randomUUID();
 
  70          * Reference to Heartbeat
 
  72         private static Heartbeat heartbeat = null;
 
  75         public int getSequenceNumber() {
 
  80         public Future<Boolean> beforeLock(String resourceId, String owner, Callback callback) {
 
  82                 TargetLock tLock = new TargetLock(resourceId, this.uuid, owner, lockProps);
 
  84                 return new LockRequestFuture(resourceId, owner, tLock.lock());
 
  89         public Boolean beforeUnlock(String resourceId, String owner) {
 
  90                 TargetLock tLock = new TargetLock(resourceId, this.uuid, owner, lockProps);
 
  92                 return tLock.unlock();
 
  96         public Boolean beforeIsLockedBy(String resourceId, String owner) {
 
  97                 TargetLock tLock = new TargetLock(resourceId, this.uuid, owner, lockProps);
 
  99                 return tLock.isActive();
 
 103         public Boolean beforeIsLocked(String resourceId) {
 
 104                 TargetLock tLock = new TargetLock(resourceId, this.uuid, "dummyOwner", lockProps);
 
 106                 return tLock.isLocked();
 
 110         public boolean afterStart(PolicyEngine engine) {
 
 113                         this.lockProps = new DistributedLockingProperties(SystemPersistence.manager.getProperties(DistributedLockingFeature.CONFIGURATION_PROPERTIES_NAME));
 
 114                 } catch (PropertyException e) {
 
 115                         logger.error("DistributedLockingFeature feature properies have not been loaded", e);
 
 116                         throw new DistributedLockingFeatureException(e);
 
 119                 long heartbeatInterval = this.lockProps.getHeartBeatIntervalProperty();
 
 122                 heartbeat = new Heartbeat(this.uuid, lockProps);
 
 124                 this.scheduledExecutorService = Executors.newScheduledThreadPool(1);
 
 125                 this.scheduledExecutorService.scheduleAtFixedRate(heartbeat, heartbeatInterval, heartbeatInterval, TimeUnit.MILLISECONDS);
 
 130          * This method kills the heartbeat thread and calls refreshLockTable which removes
 
 131          * any records from the db where the current host is the owner.
 
 134         public boolean beforeShutdown(PolicyEngine engine) {
 
 135                 scheduledExecutorService.shutdown();
 
 141          * This method removes all records owned by the current host from the db.
 
 143         private void cleanLockTable() {
 
 145             try (Connection conn = DriverManager.getConnection(lockProps.getDbUrl(), 
 
 146                         lockProps.getDbUser(),
 
 147                         lockProps.getDbPwd());
 
 148                 PreparedStatement statement = conn.prepareStatement("DELETE FROM pooling.locks WHERE host = ? OR expirationTime < ?");
 
 151                                 statement.setString(1, this.uuid.toString());
 
 152                                 statement.setLong(2, System.currentTimeMillis());
 
 153                                 statement.executeUpdate();
 
 155                 } catch (SQLException e) {
 
 156                         logger.error("error in refreshLockTable()", e);
 
 161         public static Heartbeat getHeartbeat() {