-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
60 changed files
with
3,047 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
api/src/main/java/com/alibaba/nacos/api/lock/LockService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
api/src/main/java/com/alibaba/nacos/api/lock/NacosLockFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/main/java/com/alibaba/nacos/api/lock/common/LockConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
31 changes: 31 additions & 0 deletions
31
api/src/main/java/com/alibaba/nacos/api/lock/constant/PropertyConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
112
api/src/main/java/com/alibaba/nacos/api/lock/model/LockInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
api/src/main/java/com/alibaba/nacos/api/lock/remote/AbstractLockRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.