HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Dynamic voting protocol implementation for replicated file system

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
protocolvotingfilesystemfordynamicimplementationreplicated

Problem

I recently implemented a dynamic voting protocol for a replicated filesystem. I would really appreciate it if you can review the design and some choices made with regards to sharing of Locks & shared objects.
The code is quite big hence I'm not going to post it all here. It was for a school project (already graded).

In this implementation I have an "Application" that makes read/write requests (simulates user activity) to the Replication client. The client itself implements the protocol and sends TCP requests for read/write access.
A server (TCP) exists to receive requests from other clients (in a distributed system) and modify state based on incoming requests.
Each node is both a client and server.

It would be great if you could comment on any issue you can discover in the design & multi-threading aspects.

Interactive application used to enter read/write commands in the terminal, this uses the ReplicationClient class.

```
package application;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Set;

import core.Context;
import core.OpContainer;
import core.ReplicationClient;
import comm.Server;
import filesystem.FileInfo;

public class InteractiveApplication implements Application {

static Scanner inputScanner;
Integer numberOfRequests = 1;
Integer readPercent = 100;
private ReplicationClient replicationClient;

@Override
public void runApplication() {
printWelcomeMessage();
inputScanner = new Scanner(System.in);
String command = "";
while (!(command = commandPrompt(inputScanner)).contains("exit")) {
processCommand(command);
}
inputScanner.close();
System.out.println("Exited");
}

private void processCommand(String command) {
String[] split = command.split("\\s");
String operation = "";
String fileName = "";

if(split.length == 1){
oper

Solution

if(split.length == 1){
    operation = split[0];
} else {
    operation = split[0];
    fileName = split[1];
}


Looks like you could simplify this...

operation = split[0];
if(split.length != 1){
    fileName = split[1];
}


public class Server extends Thread {


Don't extend Thread. Implement Runnable instead. By extending Thread it's not possible for constructs like thread pools to run your server. What's more, "restarting" a server isn't possible either, because a thread that has stopped cannot be restarted. You can, however, rerun a runnable on a new Thread.

Code Snippets

if(split.length == 1){
    operation = split[0];
} else {
    operation = split[0];
    fileName = split[1];
}
operation = split[0];
if(split.length != 1){
    fileName = split[1];
}
public class Server extends Thread {

Context

StackExchange Code Review Q#95607, answer score: 2

Revisions (0)

No revisions yet.