Commit d7f831b2 authored by NILANJAN DAW's avatar NILANJAN DAW

Async mode StorageService made cache flag aware. Asyc Storage no more sharing stubs.

parent fff1436f
app.name="HPDOS-Client"
app.version="0.1.4"
app.cache=1
app.thread_count=10
app.rps=1000
app.runtime=10
......
......@@ -53,7 +53,7 @@ dependencies {
application {
// Define the main class for the application.
mainClass = 'HpdosClient.ClientRunnerSync'
mainClass = 'HpdosClient.ClientRunner'
}
sourceSets {
......
......@@ -38,6 +38,7 @@ public class ClientRunnerSync {
public boolean experimentEnded = false;
private Queue<Long> createTime, updateTime, readTime, deleteTime;
private final Properties properties;
private boolean cacheEnabled;
public ClientRunnerSync() {
clientID = UUID.randomUUID().toString();
......@@ -46,6 +47,7 @@ public class ClientRunnerSync {
InputStream inputStream = new FileInputStream(propertiesFile);
properties.load(inputStream);
parallelCount = Integer.parseInt((String) properties.get("app.thread_count"));
cacheEnabled = properties.get("app.cache").equals("1");
runtime = Integer.parseInt((String) properties.get("app.runtime"));
cCreate = Integer.parseInt((String) properties.get("app.cycle_create"));
cRead = Integer.parseInt((String) properties.get("app.cycle_read"));
......@@ -79,7 +81,7 @@ public class ClientRunnerSync {
return new String(data);
}
public double runExperiment(String id, long experimentStartTime) {
StorageServiceSync storageService = new StorageServiceSync(this.clientID);
StorageServiceSync storageService = new StorageServiceSync(this.clientID, this.cacheEnabled);
storageService.initStorage();
String value = createString(), updatedValue = createString();
for (;;) {
......@@ -112,7 +114,8 @@ public class ClientRunnerSync {
if ((currentTime - experimentStartTime) >= runtime * 1000L)
break;
}
System.out.println(storageService.getCache().stats());
if (this.cacheEnabled)
System.out.println(storageService.getCache().stats());
storageService.cleanup();
return 0;
}
......@@ -174,6 +177,7 @@ public class ClientRunnerSync {
ClientRunnerSync clientRunner = new ClientRunnerSync();
System.out.println(clientRunner.getGreeting());
System.out.println("Using Sync Server. Thread count: " + parallelCount + " runtime: " + runtime + "s");
System.out.println("cache status: " + clientRunner.cacheEnabled);
ExecutorService executorService = Executors.newFixedThreadPool(parallelCount);
Thread.sleep(1000); // let things settle down a bit
Set<Callable<Double>> callables = new HashSet<>();
......
......@@ -38,6 +38,16 @@ public class StorageModel {
this.crc = crc;
}
public StorageModel(Ack ack, String owner, int accessType) {
this.version = ack.getVersion();
this.dataSize = ack.getDataSize();
this.value = ack.getValue();
this.crc = ack.getCrc();
this.key = ack.getKey();
this.accessType = accessType;
this.owner = owner;
}
public void updateData(Ack ack) {
this.version = ack.getVersion();
this.dataSize = ack.getDataSize();
......
......@@ -28,10 +28,7 @@ import io.grpc.ManagedChannelBuilder;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ExecutionException;
public class StorageServiceSync {
......@@ -40,36 +37,39 @@ public class StorageServiceSync {
private ManagedChannel masterChannel;
private ArrayList<ManagedChannel> channels;
private List<Follower> replicaSet;
private final LoadingCache<String, StorageModel> cache;
private boolean cacheEnabled;
private LoadingCache<String, StorageModel> cache = null;
public StorageServiceSync(String clientID) {
public StorageServiceSync(String clientID, boolean cacheEnabled) {
this.clientID = clientID;
this.cache = CacheBuilder
.newBuilder()
.maximumSize(10000)
.recordStats()
.build(new CacheLoader<>() {
@Override
public StorageModel load(@Nonnull String key) throws Exception {
int index = (int) (Math.random() * channels.size());
NetworkServiceGrpc.NetworkServiceBlockingStub stub =
NetworkServiceGrpc.newBlockingStub(channels.get(index));
ArrayList<Request> request = new ArrayList<>();
request.add(RequestBuilder.buildRequest(MessageConstants.METADATA_READ,
0, 0, key, 0, MessageConstants.METADATA_ACCESS_PRIVATE, clientID, ""));
Packet requestPacket = RequestBuilder.buildPacket(request);
Packet responsePacket = stub.readMetadata(requestPacket);
Response response = responsePacket.getResponse(0);
if (response.getStatus() == MessageConstants.STATUS_OK) {
Ack ack = response.getAck();
return new StorageModel(ack.getVersion(), ack.getDataSize(), ack.getKey(),
MessageConstants.METADATA_ACCESS_PRIVATE, clientID, ack.getCrc(), ack.getValue());
} else {
throw new IOException("Resource not found");
this.cacheEnabled = cacheEnabled;
if (this.cacheEnabled)
this.cache = CacheBuilder
.newBuilder()
.maximumSize(10000)
.recordStats()
.build(new CacheLoader<>() {
@Override
public StorageModel load(@Nonnull String key) throws Exception {
int index = (int) (Math.random() * channels.size());
NetworkServiceGrpc.NetworkServiceBlockingStub stub =
NetworkServiceGrpc.newBlockingStub(channels.get(index));
ArrayList<Request> request = new ArrayList<>();
request.add(RequestBuilder.buildRequest(MessageConstants.METADATA_READ,
0, 0, key, 0, MessageConstants.METADATA_ACCESS_PRIVATE, clientID, ""));
Packet requestPacket = RequestBuilder.buildPacket(request);
Packet responsePacket = stub.readMetadata(requestPacket);
Response response = responsePacket.getResponse(0);
if (response.getStatus() == MessageConstants.STATUS_OK) {
Ack ack = response.getAck();
return new StorageModel(ack.getVersion(), ack.getDataSize(), ack.getKey(),
MessageConstants.METADATA_ACCESS_PRIVATE, clientID, ack.getCrc(), ack.getValue());
} else {
throw new IOException("Resource not found");
}
}
}
});
});
}
......@@ -120,14 +120,31 @@ public class StorageServiceSync {
// read back the metadata
public Map.Entry<StorageModel, Long> read(String key) {
long timestampReadStart = System.currentTimeMillis();
try {
StorageModel model = cache.get(key);
return new AbstractMap.SimpleEntry<>(model, timestampReadStart);
} catch (ExecutionException e) {
e.printStackTrace();
if (cacheEnabled) {
long timestampReadStart = System.currentTimeMillis();
try {
StorageModel model = cache.get(key);
return new AbstractMap.SimpleEntry<>(model, timestampReadStart);
} catch (ExecutionException e) {
e.printStackTrace();
}
return new AbstractMap.SimpleEntry<>(null, timestampReadStart);
} else {
int rnd = new Random().nextInt(this.channels.size());
NetworkServiceGrpc.NetworkServiceBlockingStub stub = NetworkServiceGrpc.newBlockingStub(channels.get(rnd));
ArrayList<Request> request = new ArrayList<>();
request.add(RequestBuilder.buildRequest(MessageConstants.METADATA_READ,
0, 0, key, 0, MessageConstants.METADATA_ACCESS_PRIVATE, this.clientID, ""));
Packet packet = RequestBuilder.buildPacket(request);
long timestampReadStart = System.currentTimeMillis();
Packet responsePacket = stub.readMetadata(packet);
if (responsePacket.getResponse(0).getStatus() == MessageConstants.STATUS_OK) {
StorageModel model = new StorageModel(responsePacket.getResponse(0).getAck(),
this.clientID, MessageConstants.METADATA_ACCESS_PRIVATE);
return new AbstractMap.SimpleEntry<>(model, timestampReadStart);
}
return new AbstractMap.SimpleEntry<>(null, timestampReadStart);
}
return new AbstractMap.SimpleEntry<>(null, timestampReadStart);
}
public long update(String key, String value, int version) {
......@@ -139,7 +156,8 @@ public class StorageServiceSync {
long timestampCreateStart = System.currentTimeMillis();
NetworkServiceGrpc.NetworkServiceBlockingStub masterStub = NetworkServiceGrpc.newBlockingStub(masterChannel);
Packet responsePacket = masterStub.updateMetadata(packet);
if (responsePacket.getResponse(0).getStatus() == MessageConstants.STATUS_OK)
if (this.cacheEnabled &&
responsePacket.getResponse(0).getStatus() == MessageConstants.STATUS_OK)
cache.invalidate(key);
return timestampCreateStart;
}
......@@ -153,9 +171,9 @@ public class StorageServiceSync {
long timestampCreateStart = System.currentTimeMillis();
NetworkServiceGrpc.NetworkServiceBlockingStub masterStub = NetworkServiceGrpc.newBlockingStub(masterChannel);
Packet responsePacket = masterStub.deleteMetadata(packet);
if (responsePacket.getResponse(0).getStatus() == MessageConstants.STATUS_OK)
if (this.cacheEnabled &&
responsePacket.getResponse(0).getStatus() == MessageConstants.STATUS_OK)
cache.invalidate(key);
return timestampCreateStart;
}
}
/*
* Copyright 2021 Nilanjan Daw, Synerg Lab, Department of CSE, IIT Bombay
*
* 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.
*/
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package HpdosClient;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
public class AppTest {
@Test public void testAppHasAGreeting() {
ClientRunner classUnderTest = new ClientRunner();
assertNotNull("app should have a greeting", classUnderTest.getGreeting());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment