Skip to content

Commit

Permalink
Open Source DLM Initial Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Shantanu Tiwari authored and tshan10 committed Jul 22, 2024
0 parents commit 22a891e
Show file tree
Hide file tree
Showing 29 changed files with 4,162 additions and 0 deletions.
672 changes: 672 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions .github/workflows/sonarcloud-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17
- name: Cache SonarCloud packages
uses: actions/cache@v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=PhonePe_dlm
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
### Java template
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

hs_err_pid*
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

## Directory-based project format:
/.idea/
/target/
benchmarking/.idea/
benchmarking/target/
.classpath
.project
.settings/
.DS_Store
*.iml

/bin/
63 changes: 63 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
image: docker.phonepe.com:5000/pp-maven:3.8.4-openjdk-all

.default: &default
only:
- merge_requests
- web

.default_tag: &default_tag
tags:
- backend-docker-large

stages:
- build
- quality
- deploy
- merge
- release

build_package:
stage: build
<<: *default
<<: *default_tag
when: manual
allow_failure: false
script:
- mvn clean package -U -DskipTests=true

merge_ready:
stage: quality
only:
- merge_requests
<<: *default_tag
script:
- mvn clean verify -U sonar:sonar -Pquality_check -Dsonar.pullrequest.key=$CI_MERGE_REQUEST_IID -Dsonar.pullrequest.branch=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME -Dsonar.pullrequest.base=$CI_MERGE_REQUEST_TARGET_BRANCH_NAME
allow_failure: false

deploy_snapshot:
stage: deploy
<<: *default_tag
<<: *default
only:
- web
allow_failure: false
script:
- mvn deploy -DskipTests=true -Pdocker -Dmaven.install.skip=true
when: manual

release:
<<: *default_tag
stage: release
script:
- git remote set-url origin "[email protected]:${CI_PROJECT_PATH}.git"
- git checkout main
- git pull origin main
- git checkout develop
- git pull origin develop
- mvn -U jgitflow:release-start jgitflow:release-finish
- git push --all
- git push --tags origin
when: manual
only:
refs:
- develop
46 changes: 46 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [TBD]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
134 changes: 134 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Distributed Lock Manager (DLM)

Locking is a very common expectation in SoA, where a vulnerable entity needs to be protected for a certain duration.
And the definition of vulnerable entity changes from one client to another depending on the use-cases at hand.

Distributed Lock Manager is an easy-to-use library to achieve various modes of locking, be it - Exclusive or Limited Protected(LP).
In current version, Exclusive locking mode is supported with Aerospike as the underlying storage base by leveraging
its MVCC (MultiVersion Concurrency Control) capability.

## Add Maven Dependency

```xml
<dependency>
<groupId>com.phonepe</groupId>
<artifactId>distributed-lock-manager</artifactId>
<version>3.0.6</version>
</dependency>
```


### Usage

#### Initializing Distributed Lock Manager

##### With Aerospike as lock base

``` java
DistributedLockManager lockManager = DistributedLockManager.builder()
.clientId("CLIENT_ID")
.farmId("FA1")
.lockBase(AerospikeLockBase.builder()
.mode(LockMode.EXCLUSIVE)
.store(AerospikeStore.builder()
.aerospikeClient(aerospikeClient)
.namespace("NAMESPACE")
.setSuffix("distributed_lock")
.build())
.build())
.build();
lockManager.initialize();
```

##### With HBase as lock base

``` java
DistributedLockManager lockManager = DistributedLockManager.builder()
.clientId("CLIENT_ID")
.farmId("FA1")
.lockBase(HBaseLockBase.builder()
.mode(LockMode.EXCLUSIVE)
.store(HBaseStore.builder()
.connection(connection) // HBase connection reference
.tableName("table_name")
.build())
.build())
.build();
lockManager.initialize();
```

PS : For optimum performance, DO NOT pre-create the HBase table. Library will do it for you.


# Entity Lock Management

This library offers various methods for acquiring and releasing locks on critical entities. Below are the available methods:

1. **`tryAcquireLock(lock)`**
- Attempts immediate lock acquisition. Throws an exception if the lock is unavailable. Does not wait if the lock is currently held by another thread. The default lock duration is 90 seconds.

2. **`tryAcquireLock(lock, duration)`**
- Immediate lock acquisition attempt with a specified duration. Throws an exception if the lock is unavailable. Does not wait if the lock is held by another thread.

3. **`acquireLock(lock)`**
- Tries to acquire the lock. If the lock is held by another thread, it waits until the lock becomes available, blocking the thread. The default timeout is 90 seconds, and the lock duration defaults to 90 seconds.

4. **`acquireLock(lock, duration)`**
- Similar to `acquireLock(lock)` but allows a specified duration for the lock.

5. **`acquireLock(lock, duration, timeout)`**
- Attempts to acquire the lock and waits for a limited time for it to become available. If the lock is not acquired within the given time, it returns with a failure indication. This method blocks the thread until the lock is acquired.

## Example Usage

```java
// Representing a vulnerable entity by LOCK_ID
final Lock lock = lockManager.getLockInstance("LOCK_ID", LockLevel.DC);
try {
lockManager.tryAcquireLock(lock); // Attempts to acquire the lock for the default duration of 90 seconds
// OR lockManager.tryAcquireLock(lock, 120); // Tries to acquire the lock for 120 seconds

// Perform actions once the lock is successfully acquired.

} catch (DLSException e) {
if (ErrorCode.LOCK_UNAVAILABLE.equals(e.getErrorCode)) {
// Actions to take if the lock can't be acquired.
}
} finally {
// Verify if the lock was released successfully.
boolean released = lockManager.release(lock);
}
```

```java
// Vulnerable entity represented by LOCK_ID
// Representing a vulnerable entity by LOCK_ID
final Lock lock = lockManager.getLockInstance("LOCK_ID", LockLevel.DC);
try {
lockManager.acquireLock(lock); // Attempts to acquire the lock for the default duration of 90 seconds and waits for 90 seconds
// OR lockManager.acquireLock(lock, 30); // Tries to acquire the lock for 30 seconds, waiting for 90 seconds
// OR lockManager.acquireLock(lock, 30, 30); // Tries to acquire the lock for 30 seconds, waiting for 30 seconds

// Perform actions once the lock is successfully acquired.
} catch (DLSException e) {
if (ErrorCode.LOCK_UNAVAILABLE.equals(e.getErrorCode)) {
// Actions to take if the lock can't be acquired.
}
} finally {
// Verify if the lock was released successfully.
boolean released = lockManager.release(lock);
}
```

#### Lock Levels
* DC - Acquiring/releasing lock within a DC
* XDC - Acquiring/releasing lock across DCs.


**Caution**: Concurrently obtaining a lock on the same entity using XDC across multiple data centers may result in unexpected behavior due to the replication of data between centers. Therefore, it is recommended to utilize DC locks whenever feasible.

For XDC locks requiring strong consistency, opt for a multi-site Aerospike cluster.

#### Notes

A lock exists only within the scope of a Client represented by `CLIENT_ID`.
1 change: 1 addition & 0 deletions build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BUILD_PROJECT_VERSION=1.0.0
3 changes: 3 additions & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lombok.addLombokGeneratedAnnotation = true
lombok.anyConstructor.addConstructorProperties = true
config.stopBubbling = true
Loading

0 comments on commit 22a891e

Please sign in to comment.