Skip to content

Commit

Permalink
[ISSUE #12962]Feat develop 3.0 dlock (#12981)
Browse files Browse the repository at this point in the history
* add simple Distributed lock

# Conflicts:
#	pom.xml

* add lock unit test.

* add lock unit test.

* add lock unit test.

* update lock instance

* add javadoc

* add nacoslock snapshot.

* add property.

* update property.

* add lock auth.

* fix lock auth.

* add lockInfo DTO.

* improve log and memory lack.

* merge 'develop' into lock

* add lock query count and rt metrics

* fix compile and test

---------

Co-authored-by: 985492783 <[email protected]>
  • Loading branch information
shalk and 985492783 authored Jan 2, 2025
1 parent f0ad076 commit cd7de01
Show file tree
Hide file tree
Showing 60 changed files with 3,047 additions and 12 deletions.
13 changes: 13 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/NacosFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.lock.LockService;
import com.alibaba.nacos.api.lock.NacosLockFactory;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingMaintainFactory;
import com.alibaba.nacos.api.naming.NamingMaintainService;
Expand Down Expand Up @@ -98,4 +100,15 @@ public static NamingMaintainService createMaintainService(String serverAddr) thr
public static NamingMaintainService createMaintainService(Properties properties) throws NacosException {
return NamingMaintainFactory.createMaintainService(properties);
}

/**
* Create lock service.
*
* @param properties init param
* @return lock service
* @throws NacosException Exception
*/
public static LockService createLockService(Properties properties) throws NacosException {
return NacosLockFactory.createLockService(properties);
}
}
9 changes: 9 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ public static class Naming {
public static final String CMDB_CONTEXT_TYPE = "CMDB";
}

/**
* The constants in lock directory.
*/
public static class Lock {

public static final String LOCK_MODULE = "lock";

}

/**
* The constants in remote directory.
*/
Expand Down
80 changes: 80 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/lock/LockService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.lock;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.lock.model.LockInstance;

import java.util.Properties;

/**
* Nacos Lock Process.
*
* <p>lock => {@link LockService#lock(LockInstance)} -> {@link LockInstance#lock(LockService)} ->
* {@link LockService#remoteTryLock(LockInstance)} <br/> unLock => {@link LockService#unLock(LockInstance)} ->
* {@link LockInstance#unLock(LockService)} -> {@link LockService#remoteReleaseLock(LockInstance)}
*
* @author [email protected]
* @date 2023/8/24 19:49
*/
public interface LockService {

/**
* Real lock method expose to user to acquire the lock.<br/> It will call {@link LockInstance#lock(LockService)}
* <br/>
*
* @param instance instance
* @return Boolean
* @throws NacosException NacosException
*/
Boolean lock(LockInstance instance) throws NacosException;

/**
* Real lock method expose to user to release the lock.<br/> It will call {@link LockInstance#unLock(LockService)}
* <br/>
*
* @param instance instance
* @return Boolean
* @throws NacosException NacosException
*/
Boolean unLock(LockInstance instance) throws NacosException;

/**
* use grpc request to try lock.
*
* @param instance instance
* @return Boolean
* @throws NacosException NacosException
*/
Boolean remoteTryLock(LockInstance instance) throws NacosException;

/**
* use grpc request to release lock.
*
* @param instance instance
* @return Boolean
* @throws NacosException NacosException
*/
Boolean remoteReleaseLock(LockInstance instance) throws NacosException;

/**
* get properties.
*
* @return Properties
*/
Properties getProperties();
}
48 changes: 48 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/lock/NacosLockFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.lock;

import com.alibaba.nacos.api.exception.NacosException;

import java.lang.reflect.Constructor;
import java.util.Properties;

/**
* lock Factory.
*
* @author [email protected]
* @date 2023/8/25 0:40
*/
public class NacosLockFactory {

/**
* Create a new lock service.
*
* @param properties lock service properties
* @return new lock service
* @throws NacosException nacos exception
*/
public static LockService createLockService(Properties properties) throws NacosException {
try {
Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.lock.NacosLockService");
Constructor constructor = driverImplClass.getConstructor(Properties.class);
return (LockService) constructor.newInstance(properties);
} catch (Throwable e) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.lock.common;

/**
* lock constant.
*
* @author [email protected]
* @date 2023/8/23 15:53
*/
public class LockConstants {

public static final String NACOS_LOCK_TYPE = "NACOS_LOCK";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.lock.constant;

/**
* lock properties constants.
* @author [email protected]
* @description PropertyConstants
* @date 2023/6/28 17:38
*/
public class PropertyConstants {
public static final String LOCK_REQUEST_TIMEOUT = "lockRequestTimeout";

public static final String LOCK_DEFAULT_WAIT_TIME = "nacos.lock.default_wait_time";

public static final Long LOCK_DEFAULT_WAIT_SECOND = 10_000L;
}
112 changes: 112 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/lock/model/LockInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.lock.model;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.lock.LockService;

import java.io.Serializable;
import java.util.Map;

/**
* lock info entity.
*
* @author [email protected]
* @date 2023/6/28 2:46
*/
public class LockInstance implements Serializable {

private static final long serialVersionUID = -3460985546826875524L;

private String key;

private Long expiredTime;

private Map<String, ? extends Serializable> params;

private String lockType;

public LockInstance(String key, Long expiredTime, String lockType) {
this.key = key;
this.expiredTime = expiredTime;
this.lockType = lockType;
}

public LockInstance() {
}

public Long getExpiredTime() {
return expiredTime;
}

public void setExpiredTime(Long expiredTime) {
this.expiredTime = expiredTime;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public Map<String, ? extends Serializable> getParams() {
return params;
}

public void setParams(Map<String, ? extends Serializable> params) {
this.params = params;
}

/**
* Will call {@link LockService#remoteTryLock(LockInstance)} request grpc to get lock and do something.<br/> can be
* {@link Override} to do some client special logic.
*
* @param lockService {@link LockService}
* @return Boolean {@link Boolean}
* @throws NacosException NacosException
*/
public Boolean lock(LockService lockService) throws NacosException {
return lockService.remoteTryLock(this);
}

/**
* Will call {@link LockService#remoteReleaseLock(LockInstance)} request grpc to release lock and do something.<br/>
* can be {@link Override} to do some client special logic.
*
* @param lockService {@link LockService}
* @return Boolean {@link Boolean}
* @throws NacosException NacosException
*/
public Boolean unLock(LockService lockService) throws NacosException {
return lockService.remoteReleaseLock(this);
}

/**
* spi get lock type.
*
* @return type
*/
public String getLockType() {
return lockType;
}

public void setLockType(String lockType) {
this.lockType = lockType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.lock.remote;

import com.alibaba.nacos.api.remote.request.Request;

import static com.alibaba.nacos.api.common.Constants.Lock.LOCK_MODULE;

/**
* lock grpc request.
*
* @author [email protected]
* @description LockRequest
* @date 2023/6/29 12:00
*/
public abstract class AbstractLockRequest extends Request {

@Override
public String getModule() {
return LOCK_MODULE;
}
}
Loading

0 comments on commit cd7de01

Please sign in to comment.