From 7964e9a11e0e7a9c9a9389da350c6398311b90f4 Mon Sep 17 00:00:00 2001 From: hoang-ho Date: Tue, 16 Mar 2021 13:16:59 -0500 Subject: [PATCH] fix network typo and threading issues --- TestCase3.json | 12 +++- TestCase3EC2.json | 71 +++++++++++++++++++ doc/testVerify.md | 88 +++++++++++++++++++++--- src/main/java/com/p2p/grpc/Buyer.java | 3 +- src/main/java/com/p2p/grpc/PeerImpl.java | 4 +- src/main/java/com/p2p/grpc/Seller.java | 19 +++-- 6 files changed, 178 insertions(+), 19 deletions(-) create mode 100644 TestCase3EC2.json diff --git a/TestCase3.json b/TestCase3.json index d4f468d..bc0c1fd 100644 --- a/TestCase3.json +++ b/TestCase3.json @@ -17,6 +17,7 @@ "port": "8081", "neighbors": [ "0", + "2", "3" ] }, @@ -35,7 +36,7 @@ "port": "8083", "neighbors": [ "0", - "2", + "1", "5" ] }, @@ -56,6 +57,15 @@ "3", "6" ] + }, + { + "id": "6", + "IPAddress": "localhost", + "port": "8086", + "neighbors": [ + "4", + "5" + ] } ] } \ No newline at end of file diff --git a/TestCase3EC2.json b/TestCase3EC2.json new file mode 100644 index 0000000..3ebf65e --- /dev/null +++ b/TestCase3EC2.json @@ -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" + ] + } + ] +} \ No newline at end of file diff --git a/doc/testVerify.md b/doc/testVerify.md index 0800271..ea02113 100755 --- a/doc/testVerify.md +++ b/doc/testVerify.md @@ -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. @@ -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 diff --git a/src/main/java/com/p2p/grpc/Buyer.java b/src/main/java/com/p2p/grpc/Buyer.java index eec8437..1c3480f 100755 --- a/src/main/java/com/p2p/grpc/Buyer.java +++ b/src/main/java/com/p2p/grpc/Buyer.java @@ -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(); } /** @@ -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 diff --git a/src/main/java/com/p2p/grpc/PeerImpl.java b/src/main/java/com/p2p/grpc/PeerImpl.java index b7961c4..32b844c 100755 --- a/src/main/java/com/p2p/grpc/PeerImpl.java +++ b/src/main/java/com/p2p/grpc/PeerImpl.java @@ -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(); } /** @@ -153,6 +153,8 @@ public void floodLookUp(LookUpRequest request) { public void reverseReply(ReplyRequest request) { List 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(); diff --git a/src/main/java/com/p2p/grpc/Seller.java b/src/main/java/com/p2p/grpc/Seller.java index 17524ec..060453f 100755 --- a/src/main/java/com/p2p/grpc/Seller.java +++ b/src/main/java/com/p2p/grpc/Seller.java @@ -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; /** @@ -25,7 +27,7 @@ public class Seller extends PeerImpl { private int stock; private Server server; private static Random RANDOM = new Random(0); - private Map> replyPath; + private Map>> 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) { @@ -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(); } @@ -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 path = replyPath.get(buyerId); + List 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(); @@ -57,7 +59,6 @@ public void reply(int buyerId, PeerId seller) { stub.replyRPC(replyRequest); channel.shutdown(); logger.info("Done Reply"); - replyPath.remove(buyerId); } /** @@ -82,7 +83,15 @@ public void lookupRPC(LookUpRequest request, StreamObserver 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);