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.PreparedStatement;
24 import java.sql.SQLException;
25 import java.util.Properties;
26 import java.util.UUID;
27 import org.apache.commons.dbcp2.BasicDataSource;
28 import org.apache.commons.dbcp2.BasicDataSourceFactory;
29 import org.onap.policy.common.utils.properties.exception.PropertyException;
30 import org.onap.policy.drools.core.lock.PolicyResourceLockFeatureAPI;
31 import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
32 import org.onap.policy.drools.persistence.SystemPersistence;
33 import org.onap.policy.drools.system.PolicyEngine;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class DistributedLockingFeature implements PolicyEngineFeatureAPI, PolicyResourceLockFeatureAPI {
42 private static final Logger logger = LoggerFactory.getLogger(DistributedLockingFeature.class);
45 * Properties Configuration Name
47 public static final String CONFIGURATION_PROPERTIES_NAME = "feature-distributed-locking";
50 * Properties for locking feature
52 private DistributedLockingProperties lockProps;
55 * Data source used to connect to the DB containing locks.
57 private BasicDataSource dataSource;
62 private static final UUID uuid = UUID.randomUUID();
65 public int getSequenceNumber() {
70 public OperResult beforeLock(String resourceId, String owner, int holdSec) {
72 TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource);
74 return(tLock.lock(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
78 public OperResult beforeRefresh(String resourceId, String owner, int holdSec) {
80 TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource);
82 return(tLock.refresh(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
86 public OperResult beforeUnlock(String resourceId, String owner) {
87 TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource);
89 return(tLock.unlock() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
93 public OperResult beforeIsLockedBy(String resourceId, String owner) {
94 TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource);
96 return(tLock.isActive() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
100 public OperResult beforeIsLocked(String resourceId) {
101 TargetLock tLock = new TargetLock(resourceId, uuid, "dummyOwner", dataSource);
103 return(tLock.isLocked() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
107 public boolean afterStart(PolicyEngine engine) {
110 this.lockProps = new DistributedLockingProperties(SystemPersistence.manager.getProperties(DistributedLockingFeature.CONFIGURATION_PROPERTIES_NAME));
111 this.dataSource = makeDataSource();
112 } catch (PropertyException e) {
113 logger.error("DistributedLockingFeature feature properies have not been loaded", e);
114 throw new DistributedLockingFeatureException(e);
115 } catch(InterruptedException e) {
116 logger.error("DistributedLockingFeature failed to create data source", e);
117 Thread.currentThread().interrupt();
118 throw new DistributedLockingFeatureException(e);
119 } catch(Exception e) {
120 logger.error("DistributedLockingFeature failed to create data source", e);
121 throw new DistributedLockingFeatureException(e);
130 * @return a new, pooled data source
133 private BasicDataSource makeDataSource() throws Exception {
134 Properties props = new Properties();
135 props.put("driverClassName", lockProps.getDbDriver());
136 props.put("url", lockProps.getDbUrl());
137 props.put("username", lockProps.getDbUser());
138 props.put("password", lockProps.getDbPwd());
139 props.put("testOnBorrow", "true");
140 props.put("poolPreparedStatements", "true");
142 // additional properties are listed in the GenericObjectPool API
144 return BasicDataSourceFactory.createDataSource(props);
148 * This method kills the heartbeat thread and calls refreshLockTable which removes
149 * any records from the db where the current host is the owner.
152 public boolean beforeShutdown(PolicyEngine engine) {
158 * This method removes all records owned by the current host from the db.
160 private void cleanLockTable() {
162 try (Connection conn = dataSource.getConnection();
163 PreparedStatement statement = conn.prepareStatement(
164 "DELETE FROM pooling.locks WHERE host = ? OR expirationTime < now()");
167 statement.setString(1, uuid.toString());
168 statement.executeUpdate();
170 } catch (SQLException e) {
171 logger.error("error in refreshLockTable()", e);