From 4a9a33c17ef693e6e4ea84ee709882d6e88d27f9 Mon Sep 17 00:00:00 2001
From: ivan <ivan@ivanLaptop>
Date: Mon, 24 Aug 2015 09:54:24 +0200
Subject: [PATCH] synchronized the S3Client

---
 .../main/java/com/yahoo/ycsb/db/S3Client.java | 48 +++++++++++--------
 1 file changed, 27 insertions(+), 21 deletions(-)

diff --git a/s3/src/main/java/com/yahoo/ycsb/db/S3Client.java b/s3/src/main/java/com/yahoo/ycsb/db/S3Client.java
index 0a8bc9d9..26e851df 100644
--- a/s3/src/main/java/com/yahoo/ycsb/db/S3Client.java
+++ b/s3/src/main/java/com/yahoo/ycsb/db/S3Client.java
@@ -67,6 +67,7 @@ public class S3Client extends DB {
 	public void cleanup() throws DBException {
 		try {
 		//this.s3Client.shutdown(); //this should not be used
+		//this.s3Client = null;
 		} catch (Exception e){
 			e.printStackTrace();
 		}
@@ -93,30 +94,35 @@ public class S3Client extends DB {
     	}
 	/**
 	* Initialize any state for the storage.
-	* Called once per S3 instance; there is one S3 instance per client thread.
+	* Called once per S3 instance; If the client is not null it is re-used.
 	*/
 	@Override
 	public void init() throws DBException {
-       
-		Properties props = getProperties();
-		accessKeyId = props.getProperty("s3.accessKeyId","accessKeyId");
-		secretKey = props.getProperty("s3.secretKey","secretKey");
-		endPoint = props.getProperty("s3.endPoint","s3.amazonaws.com");
-		region = props.getProperty("s3.region","us-east-1");
-		maxErrorRetry = props.getProperty("s3.maxErrorRetry","15");
-		System.out.println("Inizializing the S3 connection");
-		s3Credentials = new BasicAWSCredentials(accessKeyId,secretKey);
-		clientConfig = new ClientConfiguration();
-		clientConfig.setMaxErrorRetry(Integer.parseInt(maxErrorRetry));
-		try {
-		s3Client = new AmazonS3Client(s3Credentials,clientConfig);
-		s3Client.setRegion(Region.getRegion(Regions.fromName(region)));
-		s3Client.setEndpoint(endPoint);
-		System.out.println("Connection successfully initialized");
-		} catch (Exception e){
-			System.err.println("Could not connect to S3 storage because: "+ e.toString());
-			e.printStackTrace();
-			return;
+       		synchronized (S3Client.class){
+			Properties props = getProperties();
+			accessKeyId = props.getProperty("s3.accessKeyId","accessKeyId");
+			secretKey = props.getProperty("s3.secretKey","secretKey");
+			endPoint = props.getProperty("s3.endPoint","s3.amazonaws.com");
+			region = props.getProperty("s3.region","us-east-1");
+			maxErrorRetry = props.getProperty("s3.maxErrorRetry","15");
+			System.out.println("Inizializing the S3 connection");
+			s3Credentials = new BasicAWSCredentials(accessKeyId,secretKey);
+			clientConfig = new ClientConfiguration();
+			clientConfig.setMaxErrorRetry(Integer.parseInt(maxErrorRetry));
+			if (s3Client != null) {
+				System.out.println("Reusing the same client");
+				return;
+			} 
+			try {
+				s3Client = new AmazonS3Client(s3Credentials,clientConfig);
+				s3Client.setRegion(Region.getRegion(Regions.fromName(region)));
+				s3Client.setEndpoint(endPoint);
+				System.out.println("Connection successfully initialized");
+			} catch (Exception e){
+				System.err.println("Could not connect to S3 storage because: "+ e.toString());
+				e.printStackTrace();
+				return;
+			}
 		}
     	}
 	/**
-- 
GitLab