Search in sources :

Example 26 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class RequestTimeoutTest method testOperationsNormalAfterPacketLoss.

/**
 * Test operations after package loss.
 * To simulate package eloss, we use iptables in linux and cut the network in windows.
 */
@Test
public void testOperationsNormalAfterPacketLoss() throws Exception {
    String key = "test-operation-after-packet-loss";
    try {
        File file = createSampleFile(key, 1024 * 1024 * 200);
        // System.out.println("start disconnect");
        ossClient.getClientConfiguration().setRequestTimeout(60 * 60 * 1000);
        try {
            ossClient.putObject(bucketName, key, file);
            Assert.fail("Get object should not be successful");
        } catch (ClientException e) {
            Assert.assertEquals(OSSErrorCode.REQUEST_TIMEOUT, e.getErrorCode());
        } finally {
            ossClient.getClientConfiguration().setRequestTimeout(requestTimeout);
        }
        ObjectListing objectListing = ossClient.listObjects(bucketName, key);
        Assert.assertEquals(objectListing.getObjectSummaries().size(), 1);
        ossClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ObjectListing(com.aliyun.oss.model.ObjectListing) ClientException(com.aliyun.oss.ClientException) File(java.io.File) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 27 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class RequestTimeoutTest method testOperationsNormalAfterTimeout.

/**
 * The connection should be reused after time out.
 */
@Test
public void testOperationsNormalAfterTimeout() throws Exception {
    String key = "test-operation-after-timeout";
    try {
        try {
            ossClient.getClientConfiguration().setRequestTimeout(1);
            ossClient.putObject(bucketName, key, TestUtils.genFixedLengthInputStream(64));
            Assert.fail("Get object should not be successful");
        } catch (ClientException e) {
            Assert.assertEquals(OSSErrorCode.REQUEST_TIMEOUT, e.getErrorCode());
        } finally {
            ossClient.getClientConfiguration().setRequestTimeout(requestTimeout);
        }
        ossClient.putObject(bucketName, key, TestUtils.genFixedLengthInputStream(64));
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        ossObject.getObjectContent().close();
        ossClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ClientException(com.aliyun.oss.ClientException) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 28 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class SwitchCredentialsAndEndpointTest method testSwitchInvalidCredentialsAndEndpoint.

@Test
public void testSwitchInvalidCredentialsAndEndpoint() {
    CredentialsProvider credsProvider = ossClient.getCredentialsProvider();
    Credentials defaultCreds = credsProvider.getCredentials();
    assertEquals(OSS_TEST_ACCESS_KEY_ID_1, defaultCreds.getAccessKeyId());
    assertEquals(OSS_TEST_ACCESS_KEY_ID_1, defaultCreds.getSecretAccessKey());
    // Switch to invalid credentials
    Credentials invalidCreds = new DefaultCredentials(INVALID_ACCESS_ID, INVALID_ACCESS_KEY);
    ossClient.switchCredentials(invalidCreds);
    // Verify invalid credentials under default endpoint
    try {
        ossClient.getBucketLocation(bucketName);
        fail("Should not be able to get bucket location with invalid credentials.");
    } catch (OSSException ex) {
        assertEquals(OSSErrorCode.INVALID_ACCESS_KEY_ID, ex.getErrorCode());
    }
    // Switch to valid endpoint
    ossClient.setEndpoint(INVALID_ENDPOINT);
    // Verify second credentials under invalid endpoint
    try {
        ossClient.getBucketLocation(bucketName);
        fail("Should not be able to get bucket location with second credentials.");
    } catch (ClientException ex) {
        assertEquals(ClientErrorCode.UNKNOWN_HOST, ex.getErrorCode());
    } finally {
        restoreDefaultCredentials();
        restoreDefaultEndpoint();
    }
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) OSSException(com.aliyun.oss.OSSException) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) ClientException(com.aliyun.oss.ClientException) Credentials(com.aliyun.oss.common.auth.Credentials) DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) Test(org.junit.Test)

Example 29 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class PerftestRunner method testRun.

public void testRun() {
    int putThreadNumber = scenario.getPutThreadNumber();
    int getThreadNumber = scenario.getGetThreadNumber();
    int getQPS = scenario.getGetQPS();
    int putQPS = scenario.getPutQPS();
    final int getInterval = getThreadNumber == 0 ? 0 : getThreadNumber * (1000 / getQPS);
    final int putInterval = putThreadNumber * (1000 / putQPS);
    final long contentLength = scenario.getContentLength();
    final String bucketName = scenario.getBucketName();
    final byte[] byteArray = chooseByteArray(contentLength);
    final String keyPrefix = "xiao-perf-test-";
    final List<String> uploadedObjects = new ArrayList<String>();
    final List<Long> putLatencyArray = new ArrayList<Long>();
    final List<Long> getLatencyArray = new ArrayList<Long>();
    final PerftestRunner self = this;
    Thread[] putThreads = new Thread[putThreadNumber];
    try {
        for (int i = 0; i < putThreadNumber; i++) {
            Runnable r = new Runnable() {

                @Override
                public void run() {
                    while (!self.hasExpired()) {
                        InputStream input = null;
                        try {
                            input = new ByteArrayInputStream(byteArray);
                            String objectKey = buildObjectKey(keyPrefix);
                            log.info("Begin put " + objectKey);
                            Date from = new Date();
                            ObjectMetadata metadata = new ObjectMetadata();
                            metadata.setContentLength(contentLength);
                            ossClient.putObject(bucketName, objectKey, input, metadata);
                            Date to = new Date();
                            long latency = to.getTime() - from.getTime();
                            if (latency < putInterval) {
                                Thread.sleep(putInterval - latency);
                            } else {
                                log.warn("Put object " + objectKey + " latency " + latency + ", exceed max interval " + putInterval);
                            }
                            log.info("Put object " + objectKey + " finished, elapsed " + latency + "ms");
                            try {
                                lock.lock();
                                uploadedObjects.add(objectKey);
                                putLatencyArray.add(latency);
                            } finally {
                                lock.unlock();
                            }
                        } catch (OSSException oe) {
                            log.warn("Unexpected oss exception occurs when putting object " + oe.getMessage());
                            recordError(OperationType.PUT);
                        } catch (ClientException ce) {
                            log.warn("Unexpected client exception occurs when putting object " + ce.getMessage());
                            recordError(OperationType.PUT);
                        } catch (Exception e) {
                            log.warn("Other unexpected exception " + e.getMessage());
                            recordError(OperationType.PUT);
                        } finally {
                            if (input != null) {
                                try {
                                    input.close();
                                    input = null;
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            };
            putThreads[i] = new Thread(r);
        }
    } catch (Exception ex) {
        log.error(ex.getMessage());
        Assert.fail(ex.getMessage());
    }
    Thread[] getThreads = new Thread[getThreadNumber];
    try {
        for (int i = 0; i < getThreadNumber; i++) {
            Runnable r = new Runnable() {

                @Override
                public void run() {
                    OSSObject o = null;
                    while (!hasExpired()) {
                        try {
                            String objectKey = null;
                            if (uploadedObjects.size() > 0) {
                                try {
                                    lock.lock();
                                    if (uploadedObjects.size() > 0) {
                                        int index = new Random().nextInt(uploadedObjects.size());
                                        objectKey = uploadedObjects.get(index);
                                    }
                                } finally {
                                    lock.unlock();
                                }
                            } else {
                                Thread.sleep(50);
                                continue;
                            }
                            GetObjectRequest request = new GetObjectRequest(bucketName, objectKey);
                            log.info("Begin get " + objectKey);
                            Date from = new Date();
                            o = ossClient.getObject(request);
                            // read data
                            byte[] content = new byte[8196];
                            InputStream in = o.getObjectContent();
                            while (in.read(content) >= 0) ;
                            Date to = new Date();
                            long latency = to.getTime() - from.getTime();
                            if (latency < getInterval) {
                                Thread.sleep(getInterval - latency);
                            } else {
                                log.warn("Get object " + objectKey + " latency " + latency + ", exceed max interval " + getInterval);
                            }
                            log.info("Get object " + objectKey + " finished, elapsed " + latency + "ms");
                            try {
                                lock.lock();
                                getLatencyArray.add(latency);
                            } finally {
                                lock.unlock();
                            }
                        } catch (OSSException oe) {
                            log.warn("Unexpected oss exception occurs when getting object " + oe.getMessage());
                            recordError(OperationType.GET);
                        } catch (ClientException ce) {
                            log.warn("Unexpected oss exception occurs when getting object " + ce.getMessage());
                            recordError(OperationType.GET);
                        } catch (Exception e) {
                            log.warn("Other unexpected exception " + e.getMessage());
                            recordError(OperationType.GET);
                        } finally {
                            if (o != null) {
                                try {
                                    o.getObjectContent().close();
                                    o = null;
                                } catch (IOException e) {
                                    log.error("IO Exception " + e.getMessage());
                                }
                            }
                        }
                    }
                }
            };
            getThreads[i] = new Thread(r);
        }
    } catch (Exception ex) {
        log.error(ex.getMessage());
        Assert.fail(ex.getMessage());
    }
    startTime = new Date();
    Thread[] allThreads = joinThreads(putThreads, getThreads);
    waitAll(allThreads);
    endTime = new Date();
    calculateResult(putLatencyArray, OperationType.PUT, putThreadNumber);
    calculateResult(getLatencyArray, OperationType.GET, getThreadNumber);
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Date(java.util.Date) JDOMException(org.jdom.JDOMException) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientException(com.aliyun.oss.ClientException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest)

Example 30 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class ExceptionFactoryTest method testCreateNetworkException.

@Test
public void testCreateNetworkException() {
    SocketTimeoutException ste = new SocketTimeoutException();
    ClientException ex = ExceptionFactory.createNetworkException(ste);
    assertEquals(ex.getErrorCode(), ClientErrorCode.SOCKET_TIMEOUT);
    ConnectTimeoutException cte = new ConnectTimeoutException();
    ex = ExceptionFactory.createNetworkException(cte);
    assertEquals(ex.getErrorCode(), ClientErrorCode.CONNECTION_TIMEOUT);
    IOException ioe = new IOException();
    ex = ExceptionFactory.createNetworkException(ioe);
    assertEquals(ex.getErrorCode(), ClientErrorCode.UNKNOWN);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ClientException(com.aliyun.oss.ClientException) IOException(java.io.IOException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) Test(org.junit.Test)

Aggregations

ClientException (com.aliyun.oss.ClientException)48 OSSException (com.aliyun.oss.OSSException)27 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 OSS (com.aliyun.oss.OSS)16 IOException (java.io.IOException)12 InputStream (java.io.InputStream)12 Test (org.junit.Test)11 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)9 OSSObject (com.aliyun.oss.model.OSSObject)9 ArrayList (java.util.ArrayList)9 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)8 File (java.io.File)7 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)6 ObjectListing (com.aliyun.oss.model.ObjectListing)5 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)5 ClientConfiguration (com.aliyun.oss.ClientConfiguration)4 ServiceException (com.aliyun.oss.ServiceException)4 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)4 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)4