Skip to content

Commit

Permalink
fix network typo and threading issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hoang-ho committed Mar 16, 2021
1 parent 5aa86c6 commit 7964e9a
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 19 deletions.
12 changes: 11 additions & 1 deletion TestCase3.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"port": "8081",
"neighbors": [
"0",
"2",
"3"
]
},
Expand All @@ -35,7 +36,7 @@
"port": "8083",
"neighbors": [
"0",
"2",
"1",
"5"
]
},
Expand All @@ -56,6 +57,15 @@
"3",
"6"
]
},
{
"id": "6",
"IPAddress": "localhost",
"port": "8086",
"neighbors": [
"4",
"5"
]
}
]
}
71 changes: 71 additions & 0 deletions TestCase3EC2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"N": "7",
"K": "3",
"network": [
{
"id": "0",
"IPAddress": "172.31.45.247",
"port": "8080",
"neighbors": [
"3",
"1"
]
},
{
"id": "1",
"IPAddress": "172.31.91.178",
"port": "8080",
"neighbors": [
"0",
"2",
"3"
]
},
{
"id": "2",
"IPAddress": "172.31.88.28",
"port": "8080",
"neighbors": [
"1",
"4"
]
},
{
"id": "3",
"IPAddress": "172.31.55.0",
"port": "8080",
"neighbors": [
"0",
"1",
"5"
]
},
{
"id": "4",
"IPAddress": "172.31.18.128",
"port": "8080",
"neighbors": [
"2",
"6"
]
},
{
"id": "5",
"IPAddress": "172.31.85.180",
"port": "8080",
"neighbors": [
"3",
"6"
]
},
{
"id": "6",
"IPAddress": "172.31.95.38",
"port": "8080",
"neighbors": [
"4",
"5"
]
}
]
}
88 changes: 77 additions & 11 deletions doc/testVerify.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
## How to make sure the test is correct?
# Testing Proces


### Milestone 3
## Milestone 3

**Steps to run locally**

### Steps to run locally

Example for config files are TestCase1.json and TestCase2.json.

Expand Down Expand Up @@ -48,37 +49,102 @@ For **TestCase3.json**, let node 0, 1, 2 be the buyer, and node 3 and 4 be the n
Open up 7 terminals and try the following:

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase2.json -id 0 -role buyer -product boar -hop 4
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3.json -id 0 -role buyer -product boar -hop 4
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase2.json -id 1 -role buyer -product boar -hop 3
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3.json -id 1 -role buyer -product boar -hop 3
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase2.json -id 2 -role buyer -product fish -hop 3
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3.json -id 2 -role buyer -product fish -hop 3
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase2.json -id 3 -role peer
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3.json -id 3 -role peer
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase2.json -id 4 -role peer
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3.json -id 4 -role peer
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase2.json -id 5 -role seller -product boar -stock 3
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3.json -id 5 -role seller -product boar -stock 3
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase2.json -id 6 -role seller -product fish -stock 3
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3.json -id 6 -role seller -product fish -stock 3
```

The test script for this test case is in doc/test/TestCase3.sh, which will run 7 processes in parallel

Steps to run EC2
### Steps to run on EC2 cluster.

1. Make sure you have key pair, here I use 677kp. Creates N EC2 instances using the following image: ami-07916b33d72291f85

```
$ aws ec2 run-instances --image-id ami-07916b33d72291f85 --instance-type t2.micro --key-name 677kp
```

2. Save the PrivateIpAddress for each peer. You can find this from the terminal output of the above command, or from sudo ifconfig when you already ssh into the EC2 instance

3. Put the PrivateIpAddress of each peer as the IPAddress in the config file. Remember that if you put PrivateIpAddress of one instance as IPAddress for a peer, you later must call that peer with the same id,
e.g. if you put IPAddress for peer 0 as "172.31.55.0", then later in the instance with that PrivateIpAddress, you need to specify the "-id 0"

4. After you replace all IPAddress, move the config file into each instance with scp

```
$ scp -i "677kp.pem" TestCase3.json ec2-user@$PublicDnsName:~/CompSci677-Lab1
```

Where $PublicDnsName is the Public DNS name of your instance, obtained from running: ```aws ec2 describe-instances --instance-id $INSTANCEID```

5. ssh into your instance:

```
$ ssh -i "677kp.pem" ec2-user@$PublicDnsName
```

6. Go to the directory

```
$ cd CompSci677-Lab1
```

7. Start the test. For the instance which you use the PrivateIpAddress for peer $ID, you need to specify that ID again when running the jar file.
For example, I use "172.31.45.247" for peer 0, later in the instance with PrivateIpAddress "172.31.45.247", I ran:

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3EC2.json -id 0 -role buyer -product boar -hop 4
```

For other peers in other EC2 instance, run the corresponding command:

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3EC2.json -id 1 -role buyer -product boar -hop 3
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3EC2.json -id 2 -role buyer -product fish -hop 3
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3EC2.json -id 3 -role peer
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3EC2.json -id 4 -role peer
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3EC2.json -id 5 -role seller -product boar -stock 3
```

```
java -jar build/libs/BuyerSellerNetwork-1.0-SNAPSHOT.jar -config TestCase3EC2.json -id 6 -role seller -product fish -stock 3
```

Feel free to let the peer running for 1-2 minutes to see the how process in action!


### Milestone 2
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/p2p/grpc/Buyer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Buyer(int id, String IPAddress, int port, int KNeighbors, Product product
this.buyItems = new ConcurrentHashMap<>();
this.potentialSellers = Collections.synchronizedList(new ArrayList<>());
this.server =
ServerBuilder.forPort(port).addService(new MarketPlaceBuyerImpl()).executor(Executors.newFixedThreadPool(KNeighbors + 1)).build();
ServerBuilder.forPort(port).addService(new MarketPlaceBuyerImpl()).executor(Executors.newFixedThreadPool(KNeighbors + 2)).build();
}

/**
Expand Down Expand Up @@ -137,6 +137,7 @@ public void startServer() {
}

public void run() {
logger.info("All neighbors of ours" + this.getAllNeighbors());
this.startServer();

// buyer keeps buying products forever
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/p2p/grpc/PeerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public PeerImpl(int id, String IPAddress, int port, int KNeighbor) {
this.KNeighbor = KNeighbor;
this.neighbors = new HashMap<>();
this.server =
ServerBuilder.forPort(port).addService(new MarketPlaceImpl()).executor(Executors.newFixedThreadPool(KNeighbor + 1)).build();
ServerBuilder.forPort(port).addService(new MarketPlaceImpl()).executor(Executors.newFixedThreadPool(KNeighbor + 2)).build();
}

/**
Expand Down Expand Up @@ -153,6 +153,8 @@ public void floodLookUp(LookUpRequest request) {

public void reverseReply(ReplyRequest request) {
List<Integer> path = new ArrayList<>(request.getPathList());
logger.info("PAth: " + path);
logger.info("All our neighbors" + getAllNeighbors());
PeerId fromNode = neighbors.get(path.remove(path.size() - 1));
PeerId seller = request.getSellerId();

Expand Down
19 changes: 14 additions & 5 deletions src/main/java/com/p2p/grpc/Seller.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.logging.Logger;

/**
Expand All @@ -25,7 +27,7 @@ public class Seller extends PeerImpl {
private int stock;
private Server server;
private static Random RANDOM = new Random(0);
private Map<Integer, List<Integer>> replyPath;
private Map<Integer, BlockingDeque<List<Integer>>> replyPath;

private static final Logger logger = Logger.getLogger(Seller.class.getName());
public Seller(int id, String IPAddress, int port, int KNeighbors, Product product, int amount) {
Expand All @@ -35,7 +37,7 @@ public Seller(int id, String IPAddress, int port, int KNeighbors, Product produc
this.stock = amount;
this.replyPath = new ConcurrentHashMap<>();
this.server =
ServerBuilder.forPort(port).addService(new MarketplaceSellerImpl()).executor(Executors.newFixedThreadPool(KNeighbors + 1)).build();
ServerBuilder.forPort(port).addService(new MarketplaceSellerImpl()).executor(Executors.newFixedThreadPool(KNeighbors + 2)).build();
}


Expand All @@ -44,7 +46,7 @@ public Seller(int id, String IPAddress, int port, int KNeighbors, Product produc
* */
@Override
public void reply(int buyerId, PeerId seller) {
List<Integer> path = replyPath.get(buyerId);
List<Integer> path = replyPath.get(buyerId).pollFirst();
int previousNodeId = path.remove(path.size() - 1);
PeerId previousNode = this.getNeighbor(previousNodeId);
ManagedChannel channel = ManagedChannelBuilder.forAddress(previousNode.getIPAddress(), previousNode.getPort()).usePlaintext().build();
Expand All @@ -57,7 +59,6 @@ public void reply(int buyerId, PeerId seller) {
stub.replyRPC(replyRequest);
channel.shutdown();
logger.info("Done Reply");
replyPath.remove(buyerId);
}

/**
Expand All @@ -82,7 +83,15 @@ public void lookupRPC(LookUpRequest request, StreamObserver<Empty> streamObserve
// if the seller is selling the product
if (request.getProduct().equals(product.name())) {
logger.info("Reply to lookup request from peer " + request.getBuyer() + " for product " + request.getProduct());
replyPath.put(request.getBuyer(), new ArrayList<>(request.getPathList()));
synchronized (this) {
if (replyPath.containsKey(request.getBuyer())) {
replyPath.get(request.getBuyer()).addLast(new ArrayList<>(request.getPathList()));
} else {
replyPath.put(request.getBuyer(), new LinkedBlockingDeque<>());
replyPath.get(request.getBuyer()).addLast(new ArrayList<>(request.getPathList()));
}
}

PeerId seller =
PeerId.newBuilder().setId(Seller.this.getId()).setIPAddress(Seller.this.getIPAddress()).setPort(Seller.this.getPort()).build();
Seller.this.reply(request.getBuyer(), seller);
Expand Down

0 comments on commit 7964e9a

Please sign in to comment.