Skip to content

Commit

Permalink
Merge pull request #287 from scalecube/feature/issue-283-cluster-code…
Browse files Browse the repository at this point in the history
…c-artifact

WIP: Issue 283 - cluster codec artifact
  • Loading branch information
artem-v authored Jan 8, 2020
2 parents ff60305 + 2809ea4 commit 00a8c84
Show file tree
Hide file tree
Showing 40 changed files with 727 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public interface Cluster {
<T> Optional<T> metadata();

/**
* Returns cluster member metadata by given member with default metadata type.
* Returns cluster member metadata by given member.
*
* @param member cluster member
* @return cluster member metadata
Expand Down
41 changes: 37 additions & 4 deletions cluster-api/src/main/java/io/scalecube/cluster/ClusterConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.scalecube.cluster.fdetector.FailureDetectorConfig;
import io.scalecube.cluster.gossip.GossipConfig;
import io.scalecube.cluster.membership.MembershipConfig;
import io.scalecube.cluster.metadata.MetadataCodec;
import io.scalecube.cluster.metadata.MetadataDecoder;
import io.scalecube.cluster.metadata.MetadataEncoder;
import io.scalecube.cluster.transport.api.TransportConfig;
Expand Down Expand Up @@ -32,6 +33,7 @@ public final class ClusterConfig implements Cloneable {

private Object metadata;
private int metadataTimeout = DEFAULT_METADATA_TIMEOUT;
private MetadataCodec metadataCodec = MetadataCodec.INSTANCE;
private MetadataEncoder metadataEncoder = MetadataEncoder.INSTANCE;
private MetadataDecoder metadataDecoder = MetadataDecoder.INSTANCE;

Expand Down Expand Up @@ -93,7 +95,7 @@ public <T> T metadata() {
}

/**
* Sets a metadata.
* Setter for metadata.
*
* @param metadata metadata
* @return new {@code ClusterConfig} instance
Expand All @@ -109,7 +111,7 @@ public int metadataTimeout() {
}

/**
* Sets a metadataTimeout.
* Setter for metadataTimeout.
*
* @param metadataTimeout metadata timeout
* @return new {@code ClusterConfig} instance
Expand All @@ -120,32 +122,62 @@ public ClusterConfig metadataTimeout(int metadataTimeout) {
return c;
}

public MetadataCodec metadataCodec() {
return metadataCodec;
}

/**
* Setter for metadataCodec.
*
* @param metadataCodec metadata codec
* @return new {@code ClusterConfig} instance
*/
public ClusterConfig metadataCodec(MetadataCodec metadataCodec) {
ClusterConfig c = clone();
c.metadataCodec = metadataCodec;
return c;
}

/**
* Deprecated since {@code 2.4.10} in favor of {@link MetadataCodec}.
*
* @return metadataEncoder
*/
@Deprecated
public MetadataEncoder metadataEncoder() {
return metadataEncoder;
}

/**
* Sets a metadataEncoder.
* Setter for metadataEncoder. Deprecated since {@code 2.4.10} in favor of {@link MetadataCodec}.
*
* @param metadataEncoder metadata encoder
* @return new {@code ClusterConfig} instance
*/
@Deprecated
public ClusterConfig metadataEncoder(MetadataEncoder metadataEncoder) {
ClusterConfig c = clone();
c.metadataEncoder = metadataEncoder;
return c;
}

/**
* Deprecated since {@code 2.4.10} in favor of {@link MetadataCodec}.
*
* @return metadataDecoder
*/
@Deprecated
public MetadataDecoder metadataDecoder() {
return metadataDecoder;
}

/**
* Sets a metadataDecoder.
* Setter for metadataDecoder. Deprecated since {@code 2.4.10} in favor of {@link MetadataCodec}.
*
* @param metadataDecoder metadata decoder
* @return new {@code ClusterConfig} instance
*/
@Deprecated
public ClusterConfig metadataDecoder(MetadataDecoder metadataDecoder) {
ClusterConfig c = clone();
c.metadataDecoder = metadataDecoder;
Expand Down Expand Up @@ -354,6 +386,7 @@ public String toString() {
return new StringJoiner(", ", ClusterConfig.class.getSimpleName() + "[", "]")
.add("metadata=" + metadataAsString())
.add("metadataTimeout=" + metadataTimeout)
.add("metadataCodec=" + metadataCodec)
.add("metadataEncoder=" + metadataEncoder)
.add("metadataDecoder=" + metadataDecoder)
.add("memberAlias='" + memberAlias + "'")
Expand Down
38 changes: 35 additions & 3 deletions cluster-api/src/main/java/io/scalecube/cluster/Member.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package io.scalecube.cluster;

import io.scalecube.net.Address;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Objects;
import java.util.UUID;

/**
* Cluster member which represents node in the cluster and contains its id and address. This class
* is essentially immutable.
*/
public final class Member {
public final class Member implements Externalizable {

private static final long serialVersionUID = 1L;

private String id;
private String alias;
private Address address;

/** Instantiates empty member for deserialization purpose. */
Member() {}
public Member() {}

/**
* Constructor.
Expand Down Expand Up @@ -63,6 +68,33 @@ public int hashCode() {
return Objects.hash(id, address);
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
// id
out.writeUTF(id);
// alias
boolean aliasNotNull = alias != null;
out.writeBoolean(aliasNotNull);
if (aliasNotNull) {
out.writeUTF(alias);
}
// address
out.writeUTF(address.toString());
}

@Override
public void readExternal(ObjectInput in) throws IOException {
// id
id = in.readUTF();
// alias
boolean aliasNotNull = in.readBoolean();
if (aliasNotNull) {
alias = in.readUTF();
}
// address
address = Address.from(in.readUTF());
}

@Override
public String toString() {
if (alias == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.scalecube.cluster.metadata;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import reactor.core.Exceptions;

public class JdkMetadataCodec implements MetadataCodec {

@Override
public Object deserialize(ByteBuffer buffer) {
byte[] bytes = buffer.array();
try (ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
return is.readObject();
} catch (Exception e) {
throw Exceptions.propagate(e);
}
}

@Override
public ByteBuffer serialize(Object metadata) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream os = new ObjectOutputStream(baos)) {
os.writeObject(metadata);
os.flush();
return ByteBuffer.wrap(baos.toByteArray());
} catch (Exception e) {
throw Exceptions.propagate(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.scalecube.cluster.metadata;

import io.scalecube.utils.ServiceLoaderUtil;
import java.nio.ByteBuffer;

/** Contains methods for metadata serializing/deserializing logic. */
public interface MetadataCodec {

MetadataCodec INSTANCE =
ServiceLoaderUtil.findFirst(MetadataCodec.class).orElseGet(JdkMetadataCodec::new);

/**
* Deserializes metadata from buffer.
*
* @param buffer metadata buffer; if {@code buffer} is empty then returned result shall be null.
* @return metadata object from metadata buffer or null
*/
Object deserialize(ByteBuffer buffer);

/**
* Serializes given metadata into buffer.
*
* @param metadata metadata object (optional); if {@code metadata} is null then returned result
* may be null or empty buffer.
* @return buffer or null
*/
ByteBuffer serialize(Object metadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import io.scalecube.utils.ServiceLoaderUtil;
import java.nio.ByteBuffer;

/**
* MetadataDecoder. <br>
* Deprecated since {@code 2.4.10} in favor of {@link MetadataCodec}.
*/
@Deprecated
@FunctionalInterface
public interface MetadataDecoder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import java.nio.ByteBuffer;
import reactor.util.annotation.Nullable;

/**
* MetadataEncoder. <br>
* Deprecated since {@code 2.4.10} in favor of {@link MetadataCodec}.
*/
@Deprecated
@FunctionalInterface
public interface MetadataEncoder {

Expand Down
11 changes: 2 additions & 9 deletions cluster-testlib/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?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">
<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">
<parent>
<artifactId>scalecube-cluster-parent</artifactId>
<groupId>io.scalecube</groupId>
Expand All @@ -21,14 +22,6 @@
<artifactId>scalecube-cluster-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>

</project>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 00a8c84

Please sign in to comment.