Skip to content

feat: coordination recipes example #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions coordination/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>tech.ydb.examples</groupId>
<artifactId>ydb-sdk-examples</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>

<groupId>tech.ydb.coordination.examples</groupId>
<artifactId>ydb-coordination-examples</artifactId>

<name>YDB SDK Coordination Service examples</name>

<packaging>pom</packaging>

<modules>
<module>recipes</module>
</modules>
</project>
58 changes: 58 additions & 0 deletions coordination/recipes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>tech.ydb.coordination.examples</groupId>
<artifactId>ydb-coordination-examples</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>ydb-coordination-recipes-example</artifactId>
<name>YDB Coordination Service recipes example</name>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>

<dependency>
<groupId>tech.ydb</groupId>
<artifactId>ydb-sdk-coordination</artifactId>
</dependency>
<dependency>
<groupId>tech.ydb.auth</groupId>
<artifactId>yc-auth-provider</artifactId>
</dependency>
</dependencies>

<build>
<finalName>jdbc-coordination-recipes-example</finalName>
<plugins>
<!-- copy dependencies to libs folder -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<!-- add libs folder to classpath -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>tech.ydb.coordination.recipes.example.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package tech.ydb.coordination.recipes.example;

import tech.ydb.coordination.CoordinationClient;
import tech.ydb.coordination.recipes.example.lib.locks.InterProcessLock;
import tech.ydb.coordination.recipes.example.lib.locks.InterProcessMutex;

import java.time.Duration;
import java.util.Scanner;

public class LockApp {

InterProcessLock lock;

LockApp(CoordinationClient client) {
client.createNode("examples/app").join().expectSuccess("cannot create coordination path");
lock = new InterProcessMutex(
client,
"examples/app",
"data".getBytes(),
"default_lock"
);
}

public void lock(Duration duration) {
try {
if (duration == null) {
lock.acquire();
} else {
lock.acquire(duration);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void release() {
try {
lock.release();
} catch (Exception e) {
e.printStackTrace();
}
}

private boolean isAcquired() {
return lock.isAcquiredInThisProcess();
}

public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter commands: lock [seconds] | release | reconnect | ?");

while (scanner.hasNextLine()) {
String commandLine = scanner.nextLine().trim();
String[] commandParts = commandLine.split("\\s+");
String command = commandParts[0];

switch (command.toLowerCase()) {
case "lock":
int seconds = -1;
if (commandParts.length > 1) {
try {
seconds = Integer.parseInt(commandParts[1]);
} catch (NumberFormatException e) {
System.out.println("Invalid number format, defaulting to 0 seconds");
}
}
if (seconds == -1) {
lock(null);
} else {
lock(Duration.ofSeconds(seconds));
}
break;
case "release":
release();
break;
case "?":
System.out.println("Lock is acquired: " + isAcquired());
break;
default:
System.out.println("Unknown command: " + command);
}
}

scanner.close();
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package tech.ydb.coordination.recipes.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tech.ydb.auth.iam.CloudAuthHelper;
import tech.ydb.coordination.CoordinationClient;
import tech.ydb.core.grpc.GrpcTransport;

public class Main {
private final static Logger logger = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java -jar jdbc-coordination-api-example.jar <connection_url>");
return;
}

try (GrpcTransport transport = GrpcTransport.forConnectionString(args[0])
.withAuthProvider(CloudAuthHelper.getAuthProviderFromEnviron())
.build()) {

logger.info("run lock app example");
CoordinationClient client = CoordinationClient.newClient(transport);
LockApp lockApp = new LockApp(client);
lockApp.run();
logger.info("lock app example finished");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tech.ydb.coordination.recipes.example.lib.election;

public interface LeaderElectionListener {
void takeLeadership() throws Exception;
}
Loading