Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#251: ability to specify offloadable executor for Hazelcast
- Loading branch information
1 parent
41cc370
commit a226652
Showing
4 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...t/src/main/java/io/github/bucket4j/grid/hazelcast/HazelcastOffloadableEntryProcessor.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,46 @@ | ||
/*- | ||
* ========================LICENSE_START================================= | ||
* Bucket4j | ||
* %% | ||
* Copyright (C) 2015 - 2020 Vladimir Bukhtoyarov | ||
* %% | ||
* 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. | ||
* =========================LICENSE_END================================== | ||
*/ | ||
package io.github.bucket4j.grid.hazelcast; | ||
|
||
import com.hazelcast.core.Offloadable; | ||
import io.github.bucket4j.distributed.remote.Request; | ||
|
||
public class HazelcastOffloadableEntryProcessor<K, T> extends HazelcastEntryProcessor<K, T> implements Offloadable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private final String executorName; | ||
|
||
public HazelcastOffloadableEntryProcessor(Request<T> request, String executorName) { | ||
super(request); | ||
this.executorName = executorName; | ||
} | ||
|
||
public HazelcastOffloadableEntryProcessor(byte[] requestBytes, String executorName) { | ||
super(requestBytes); | ||
this.executorName = executorName; | ||
} | ||
|
||
@Override | ||
public String getExecutorName() { | ||
return executorName; | ||
} | ||
|
||
} |
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
76 changes: 76 additions & 0 deletions
76
...b/bucket4j/grid/hazelcast/serialization/HazelcastOffloadableEntryProcessorSerializer.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,76 @@ | ||
/*- | ||
* ========================LICENSE_START================================= | ||
* Bucket4j | ||
* %% | ||
* Copyright (C) 2015 - 2020 Vladimir Bukhtoyarov | ||
* %% | ||
* 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. | ||
* =========================LICENSE_END================================== | ||
*/ | ||
package io.github.bucket4j.grid.hazelcast.serialization; | ||
|
||
import com.hazelcast.nio.ObjectDataInput; | ||
import com.hazelcast.nio.ObjectDataOutput; | ||
import com.hazelcast.nio.serialization.StreamSerializer; | ||
import com.hazelcast.nio.serialization.TypedStreamDeserializer; | ||
import io.github.bucket4j.grid.hazelcast.HazelcastEntryProcessor; | ||
import io.github.bucket4j.grid.hazelcast.HazelcastOffloadableEntryProcessor; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
public class HazelcastOffloadableEntryProcessorSerializer implements StreamSerializer<HazelcastOffloadableEntryProcessor>, TypedStreamDeserializer<HazelcastOffloadableEntryProcessor> { | ||
|
||
private final int typeId; | ||
|
||
public HazelcastOffloadableEntryProcessorSerializer(int typeId) { | ||
this.typeId = typeId; | ||
} | ||
|
||
public Class<HazelcastEntryProcessor> getSerializableType() { | ||
return HazelcastEntryProcessor.class; | ||
} | ||
|
||
@Override | ||
public int getTypeId() { | ||
return typeId; | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
|
||
} | ||
|
||
@Override | ||
public void write(ObjectDataOutput out, HazelcastOffloadableEntryProcessor serializable) throws IOException { | ||
out.writeByteArray(serializable.getRequestBytes()); | ||
out.writeString(serializable.getExecutorName()); | ||
} | ||
|
||
@Override | ||
public HazelcastOffloadableEntryProcessor read(ObjectDataInput in) throws IOException { | ||
return read0(in); | ||
} | ||
|
||
@Override | ||
public HazelcastOffloadableEntryProcessor read(ObjectDataInput in, Class aClass) throws IOException { | ||
return read0(in); | ||
} | ||
|
||
private HazelcastOffloadableEntryProcessor read0(ObjectDataInput in) throws IOException { | ||
byte[] commandBytes = in.readByteArray(); | ||
String executorName = in.readString(); | ||
return new HazelcastOffloadableEntryProcessor(commandBytes, executorName); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...o/github/bucket4j/hazelcast/HazelcastWithCustomSerializersAndOffloadableExecutorTest.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,81 @@ | ||
package io.github.bucket4j.hazelcast; | ||
|
||
import com.hazelcast.config.Config; | ||
import com.hazelcast.config.JoinConfig; | ||
import com.hazelcast.core.Hazelcast; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.map.IMap; | ||
import io.github.bucket4j.distributed.proxy.ClientSideConfig; | ||
import io.github.bucket4j.distributed.proxy.ProxyManager; | ||
import io.github.bucket4j.grid.hazelcast.HazelcastProxyManager; | ||
import io.github.bucket4j.tck.AbstractDistributedBucketTest; | ||
import org.gridkit.nanocloud.Cloud; | ||
import org.gridkit.nanocloud.CloudFactory; | ||
import org.gridkit.nanocloud.VX; | ||
import org.gridkit.vicluster.ViNode; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
|
||
import java.io.Serializable; | ||
import java.util.UUID; | ||
|
||
public class HazelcastWithCustomSerializersAndOffloadableExecutorTest extends AbstractDistributedBucketTest<String> { | ||
|
||
private static IMap<String, byte[]> map; | ||
private static Cloud cloud; | ||
private static ViNode server; | ||
|
||
private static HazelcastInstance hazelcastInstance; | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
// start separated JVM on current host | ||
cloud = CloudFactory.createCloud(); | ||
cloud.node("**").x(VX.TYPE).setLocal(); | ||
server = cloud.node("stateful-hazelcast-server"); | ||
|
||
server.exec((Runnable & Serializable) () -> { | ||
Config config = new Config(); | ||
HazelcastProxyManager.addCustomSerializers(config.getSerializationConfig(), 10_000); | ||
JoinConfig joinConfig = config.getNetworkConfig().getJoin(); | ||
joinConfig.getMulticastConfig().setEnabled(false); | ||
joinConfig.getTcpIpConfig().setEnabled(true); | ||
joinConfig.getTcpIpConfig().addMember("127.0.0.1:5702"); | ||
config.setLiteMember(false); | ||
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config); | ||
hazelcastInstance.getMap("my_buckets"); | ||
}); | ||
|
||
// start hazelcast client which works inside current JVM and does not hold data | ||
Config config = new Config(); | ||
JoinConfig joinConfig = config.getNetworkConfig().getJoin(); | ||
joinConfig.getMulticastConfig().setEnabled(false); | ||
joinConfig.getTcpIpConfig().setEnabled(true); | ||
joinConfig.getTcpIpConfig().addMember("127.0.0.1:5701"); | ||
HazelcastProxyManager.addCustomSerializers(config.getSerializationConfig(), 10_000); | ||
config.setLiteMember(true); | ||
hazelcastInstance = Hazelcast.newHazelcastInstance(config); | ||
map = hazelcastInstance.getMap("my_buckets"); | ||
} | ||
|
||
@AfterClass | ||
public static void shutdown() { | ||
if (hazelcastInstance != null) { | ||
hazelcastInstance.shutdown(); | ||
} | ||
if (cloud != null) { | ||
cloud.shutdown(); | ||
} | ||
} | ||
|
||
@Override | ||
protected ProxyManager<String> getProxyManager() { | ||
return new HazelcastProxyManager<>(map, ClientSideConfig.getDefault(), "my-executor"); | ||
} | ||
|
||
@Override | ||
protected String generateRandomKey() { | ||
return UUID.randomUUID().toString(); | ||
} | ||
|
||
} |