This commit is contained in:
eson 2022-05-23 01:40:43 +08:00
parent 5deaca30ce
commit 7064f5ddbe
4 changed files with 206 additions and 75 deletions

164
pom.xml
View File

@ -1,90 +1,105 @@
<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>
<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>
<groupId>com.yuandian.dataflow</groupId>
<artifactId>dataflow</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<groupId>com.yuandian.dataflow</groupId>
<artifactId>dataflow</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dataflow</name>
<url>http://maven.apache.org</url>
<name>dataflow</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
<protobuf.version>3.20.1</protobuf.version>
<protostuff.version>1.7.4</protostuff.version>
<javax.annotation.version>1.3.2</javax.annotation.version>
<grpc.version>1.46.0</grpc.version>
<slf4j.version>1.7.36</slf4j.version>
<jraft.version>1.3.10</jraft.version>
</properties>
<grpc.version>1.45.1</grpc.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 添加grpc相关依赖包 -->
<dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>jraft-core</artifactId>
<version>${jraft.version}</version>
</dependency>
<!-- protobuf 依赖 -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax.annotation.version}</version>
</dependency>
<!-- 添加grpc相关依赖包 -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.46.0</version>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.46.0</version>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.46.0</version>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>1.46.0</version>
</dependency>
<!-- proto自动生成java文件所需的编译插件 -->
</dependencies>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>${grpc.version}</version>
</dependency>
<!-- proto自动生成java文件所需的编译插件 -->
</dependencies>
<build>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
@ -94,28 +109,33 @@
</extensions>
<plugins>
<plugin>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</plugin>
<plugin>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration> <protocArtifact>com.google.protobuf:protoc:3.13.0:exe:${os.detected.classifier}</protocArtifact>
<configuration>
<pluginId>grpc-java</pluginId>
<protocArtifact>com.google.protobuf:protoc:3.13.0:exe:${os.detected.classifier}</protocArtifact>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.46.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
<goal>test-compile</goal>
<goal>test-compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
</project>

View File

@ -1,4 +1,7 @@
#! /bin/bash
OUTPUT_FILE="./src/main/java/"
# OUTPUT_FILE="./src/main/java/"
protoc src/main/java/com/yuandian/dataflow/rpc/*.proto --plugin=protoc-gen-grpc-java --java_out=$OUTPUT_FILE --grpc-java_out=$OUTPUT_FILE
# for PROTOFILE in `find ./src -name "*.proto"`
# do
# protoc $PROTOFILE --plugin=/home/eson/workspace/dataflow/target/protoc-plugins --java_out=$OUTPUT_FILE --grpc-java_out=$OUTPUT_FILE
# done

View File

@ -1,13 +1,22 @@
package com.yuandian.dataflow;
import com.yuandian.dataflow.rpc.DataFlowGrpc;
import com.yuandian.dataflow.rpc.DataFlowGrpc.DataFlowImplBase;
/**
* Hello world!
*
*/
public class Server
{
public class ServerImpl extends DataFlowImplBase {
}
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
}

View File

@ -0,0 +1,99 @@
package com.yuandian.dataflow.statemachine;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alipay.remoting.exception.CodecException;
import com.alipay.remoting.serialization.SerializerManager;
import com.alipay.sofa.jraft.Closure;
import com.alipay.sofa.jraft.Iterator;
import com.alipay.sofa.jraft.Status;
import com.alipay.sofa.jraft.core.StateMachineAdapter;
import com.alipay.sofa.jraft.error.RaftError;
import com.alipay.sofa.jraft.error.RaftException;
import com.alipay.sofa.jraft.storage.snapshot.SnapshotReader;
import com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter;
import com.alipay.sofa.jraft.util.Utils;
/**
* Counter state machine.
*
* @author boyan (boyan@alibaba-inc.com)
*
* 2018-Apr-09 4:52:31 PM
*/
public class StateMachine extends StateMachineAdapter {
private static final Logger LOG = LoggerFactory.getLogger(StateMachine.class);
/**
* Counter value
*/
private final AtomicLong value = new AtomicLong(0);
/**
* Leader term
*/
private final AtomicLong leaderTerm = new AtomicLong(-1);
public boolean isLeader() {
return this.leaderTerm.get() > 0;
}
/**
* Returns current value.
*/
public long getValue() {
return this.value.get();
}
@Override
public void onApply(final Iterator iter) {
while (iter.hasNext()) {
if (iter.done() != null) {
// This task is applied by this node, get value from closure to avoid additional parsing.
} else {
// Have to parse FetchAddRequest from this user log.
}
iter.next();
}
}
@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
return;
}
@Override
public void onError(final RaftException e) {
LOG.error("Raft error: {}", e, e);
}
@Override
public boolean onSnapshotLoad(final SnapshotReader reader) {
return true;
}
@Override
public void onLeaderStart(final long term) {
this.leaderTerm.set(term);
super.onLeaderStart(term);
}
@Override
public void onLeaderStop(final Status status) {
this.leaderTerm.set(-1);
super.onLeaderStop(status);
}
}