static boolean isBlobAlreadyExistsConflict(StorageException e) { if (e.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT && StorageErrorCodeStrings.BLOB_ALREADY_EXISTS.equals(e.getErrorCode())) { return true; } return false; }
/** * Writes b.length bytes from the specified byte array to this output * stream. The general contract for write(b) is that it should have exactly * the same effect as the call write(b, 0, b.length). * * @param b * Block of bytes to be written to the output stream. */ @Override public void write(byte[] b) throws IOException { try { out.write(b); } catch(IOException e) { if (e.getCause() instanceof StorageException) { StorageException storageExcp = (StorageException) e.getCause(); LOG.error("Encountered Storage Exception for write on Blob : {}" + " Exception details: {} Error Code : {}", key, e.getMessage(), storageExcp.getErrorCode()); } throw e; } }
/** * Deletes rename pending metadata file * @param fs -- the file system * @param redoFile - rename pending metadata file path * @throws IOException - If deletion fails */ @VisibleForTesting void deleteRenamePendingFile(FileSystem fs, Path redoFile) throws IOException { try { fs.delete(redoFile, false); } catch (IOException e) { // If the rename metadata was not found then somebody probably // raced with us and finished the delete first Throwable t = e.getCause(); if (t != null && t instanceof StorageException && "BlobNotFound".equals(((StorageException) t).getErrorCode())) { LOG.warn("rename pending file " + redoFile + " is already deleted"); } else { throw e; } } }
/** * Writes the specified byte to this output stream. The general contract for * write is that one byte is written to the output stream. The byte to be * written is the eight low-order bits of the argument b. The 24 high-order * bits of b are ignored. * * @param b * 32-bit integer of block of 4 bytes */ @Override public void write(int b) throws IOException { try { out.write(b); } catch(IOException e) { if (e.getCause() instanceof StorageException) { StorageException storageExcp = (StorageException) e.getCause(); LOG.error("Encountered Storage Exception for write on Blob : {}" + " Exception details: {} Error Code : {}", key, e.getMessage(), storageExcp.getErrorCode()); } throw e; } }
public static boolean isFileNotFoundException(StorageException e) { String errorCode = e.getErrorCode(); if (errorCode != null && (errorCode.equals(StorageErrorCodeStrings.BLOB_NOT_FOUND) || errorCode.equals(StorageErrorCodeStrings.RESOURCE_NOT_FOUND) || errorCode.equals(StorageErrorCodeStrings.CONTAINER_NOT_FOUND) || errorCode.equals(StorageErrorCode.BLOB_NOT_FOUND.toString()) || errorCode.equals(StorageErrorCode.RESOURCE_NOT_FOUND.toString()) || errorCode.equals(StorageErrorCode.CONTAINER_NOT_FOUND.toString()))) { return true; } return false; }
@Override public synchronized int read(byte[] b, int off, int len) throws FileNotFoundException, IOException { try { int result = 0; result = in.read(b, off, len); if (result > 0) { pos += result; } if (null != statistics && result > 0) { statistics.incrementBytesRead(result); } // Return to the caller with the result. return result; } catch(IOException e) { Throwable innerException = NativeAzureFileSystemHelper.checkForAzureStorageException(e); if (innerException instanceof StorageException) { LOG.error("Encountered Storage Exception for read on Blob : {}" + " Exception details: {} Error Code : {}", key, e, ((StorageException) innerException).getErrorCode()); if (NativeAzureFileSystemHelper.isFileNotFoundException((StorageException) innerException)) { throw new FileNotFoundException(String.format("%s is not found", key)); } } throw e; } }
private void appendBlock(ByteArrayInputStream blockData, long offset, long writeLength) { final CloudAppendBlob blobRef = (CloudAppendBlob) this.parentBlobRef; this.accessCondition.setIfAppendPositionEqual(offset); int previousResultsCount = this.opContext.getRequestResults().size(); try { blobRef.appendBlock(blockData, writeLength, this.accessCondition, this.options, this.opContext); } catch (final IOException e) { this.lastError = e; } catch (final StorageException e) { if (this.options.getAbsorbConditionalErrorsOnRetry() && e.getHttpStatusCode() == HttpURLConnection.HTTP_PRECON_FAILED && e.getExtendedErrorInformation() != null && e.getErrorCode() != null && (e.getErrorCode() .equals(StorageErrorCodeStrings.INVALID_APPEND_POSITION) || e.getErrorCode().equals(StorageErrorCodeStrings.INVALID_MAX_BLOB_SIZE_CONDITION)) && (this.opContext.getRequestResults().size() - previousResultsCount > 1)) { // Pre-condition failure on a retry should be ignored in a single writer scenario since // the request succeeded in the first attempt. Logger.info(this.opContext, SR.PRECONDITION_FAILURE_IGNORED); } else { this.lastError = Utility.initIOException(e); } } }
/** * This method create a queue if it doesn't exist */ public boolean createQueueIfNotExists(String queueName) throws InvalidKeyException, URISyntaxException, StorageException { CloudQueueClient client = connection.getCloudStorageAccount().createCloudQueueClient(); CloudQueue queueRef = client.getQueueReference(queueName); boolean creationResult; try { creationResult = queueRef.createIfNotExists(null, AzureStorageUtils.getTalendOperationContext()); } catch (StorageException e) { if (!e.getErrorCode().equals(StorageErrorCodeStrings.QUEUE_BEING_DELETED)) { throw e; } LOGGER.warn(messages.getMessage("error.QueueDeleted", queueRef.getName())); // Documentation doesn't specify how many seconds at least to wait. // 40 seconds before retrying. // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/delete-queue3 try { Thread.sleep(40000); } catch (InterruptedException eint) { throw new RuntimeException(messages.getMessage("error.InterruptedException")); } creationResult = queueRef.createIfNotExists(null, AzureStorageUtils.getTalendOperationContext()); LOGGER.debug(messages.getMessage("debug.QueueCreated", queueRef.getName())); } return creationResult; }
/** * Try to upload with a bad lease * * @param length * @param blobRef * @throws IOException */ private void tryUploadWithBadLease(final int length, final CloudBlob blobRef, final AccessCondition leaseCondition, final String expectedError) throws IOException { try { blobRef.upload(BlobTestHelper.getRandomDataStream(length), -1, leaseCondition, null, null); fail("Did not throw expected exception"); } catch (final StorageException ex) { assertEquals(ex.getHttpStatusCode(), 412); assertEquals(ex.getErrorCode(), expectedError); } }
/** * Start copying a blob and then abort */ @Test public void testCopyFromAppendBlobAbortTest() throws StorageException, URISyntaxException, IOException { final int length = 512; CloudAppendBlob originalBlob = (CloudAppendBlob) BlobTestHelper.uploadNewBlob( this.container, BlobType.APPEND_BLOB, "originalBlob", length, null); CloudAppendBlob copyBlob = this.container.getAppendBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); try { copyBlob.abortCopy(copyBlob.getProperties().getCopyState().getCopyId()); } catch (StorageException e) { if (!e.getErrorCode().contains("NoPendingCopyOperation")) { throw e; } } }
/** * Start copying a blob and then abort * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void testCopyFromPageBlobAbortTest() throws StorageException, URISyntaxException, IOException { final int length = 512; CloudPageBlob originalBlob = (CloudPageBlob) BlobTestHelper.uploadNewBlob( this.container, BlobType.PAGE_BLOB, "originalBlob", length, null); CloudPageBlob copyBlob = this.container.getPageBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); try { copyBlob.abortCopy(copyBlob.getProperties().getCopyState().getCopyId()); } catch (StorageException e) { if (!e.getErrorCode().contains("NoPendingCopyOperation")) { throw e; } } }
/** * Start copying a blob and then abort * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testCopyFromBlobAbortTest() throws StorageException, URISyntaxException, IOException { final int length = 128; CloudBlockBlob originalBlob = (CloudBlockBlob) BlobTestHelper.uploadNewBlob( this.container, BlobType.BLOCK_BLOB, "originalBlob", length, null); CloudBlockBlob copyBlob = this.container.getBlockBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); try { copyBlob.abortCopy(copyBlob.getProperties().getCopyState().getCopyId()); } catch (StorageException e) { if (!e.getErrorCode().contains("NoPendingCopyOperation")) { throw e; } } }
/** * Start copying a file and then abort * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testCopyFileAbort() throws StorageException, URISyntaxException, IOException { final int length = 128; CloudFile originalFile = FileTestHelper.uploadNewFile(this.share, length, null); CloudFile copyFile = this.share.getRootDirectoryReference().getFileReference(originalFile.getName() + "copyed"); copyFile.startCopy(originalFile); try { copyFile.abortCopy(copyFile.getProperties().getCopyState().getCopyId()); fail(); } catch (StorageException e) { if (!e.getErrorCode().contains("NoPendingCopyOperation")) { throw e; } } }
/** * Create a container * * @throws StorageException * @throws URISyntaxException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testCloudBlobContainerCreate() throws StorageException { this.container.create(); try { this.container.create(); fail("Should not be able to create twice."); } catch (StorageException e) { assertEquals(e.getErrorCode(), "ContainerAlreadyExists"); assertEquals(e.getHttpStatusCode(), 409); assertEquals(e.getMessage(), "The specified container already exists."); } }
/** * Try to create a share after it is created * * @throws StorageException */ @Test public void testCloudFileShareCreate() throws StorageException { this.share.create(); assertTrue(this.share.exists()); try { this.share.create(); fail("Share already existed but was created anyway."); } catch (StorageException e) { assertEquals(e.getErrorCode(), "ShareAlreadyExists"); assertEquals(e.getHttpStatusCode(), 409); assertEquals(e.getMessage(), "The specified share already exists."); } }
@Test public void testCloudBlobContainerDeleteIfExistsErrorCode() throws StorageException { try { container.delete(); fail("Container should not already exist."); } catch (StorageException e) { assertEquals(StorageErrorCodeStrings.CONTAINER_NOT_FOUND, e.getErrorCode()); } OperationContext ctx = new OperationContext(); ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() { @Override public void eventOccurred(SendingRequestEvent eventArg) { if (((HttpURLConnection) eventArg.getConnectionObject()).getRequestMethod().equals("DELETE")) { try { container.delete(); assertFalse(container.exists()); } catch (StorageException e) { fail("Delete should succeed."); } } } }); container.create(); // Container deletes succeed before garbage collection occurs. assertTrue(container.deleteIfExists(null, null, ctx)); }
@Test public void testTableCreateAndAttemptCreateOnceExists() throws StorageException, URISyntaxException { CloudTableClient tClient = TableTestHelper.createCloudTableClient(); String tableName = TableTestHelper.generateRandomTableName(); CloudTable table = tClient.getTableReference(tableName); tClient.getDefaultRequestOptions().setTablePayloadFormat(TablePayloadFormat.Json); try { table.create(); assertTrue(table.exists()); // Should fail as it already exists try { table.create(); fail(); } catch (StorageException ex) { assertEquals(ex.getErrorCode(), "TableAlreadyExists"); } } finally { // cleanup table.deleteIfExists(); } }
@Test public void testDeleteShareSnapshotOptions() throws StorageException, URISyntaxException, IOException { // create share with metadata this.share.create(); assertTrue(this.share.exists()); // verify that exists() call on snapshot populates metadata CloudFileShare snapshot = this.share.createSnapshot(); CloudFileClient client = FileTestHelper.createCloudFileClient(); CloudFileShare snapshotRef = client.getShareReference(snapshot.name, snapshot.snapshotID); assertTrue(snapshotRef.exists()); try { share.delete(); } catch (final StorageException e) { assertEquals(StorageErrorCodeStrings.SHARE_HAS_SNAPSHOTS, e.getErrorCode()); } share.delete(DeleteShareSnapshotsOption.INCLUDE_SNAPSHOTS, null, null, null); assertFalse(share.exists()); assertFalse(snapshot.exists()); }
/** * Ensures you cannot create a file without first creating the directory it is located within. * * @throws URISyntaxException * @throws StorageException */ public void testCloudFileDirectoryFileCreateWithoutDirectory() throws URISyntaxException, StorageException { CloudFileDirectory dir = this.share.getRootDirectoryReference().getDirectoryReference("Dir1"); CloudFile file = dir.getFileReference("file1"); try { file.create(0); fail("File shouldn't be created in a directory that wasn't created."); } catch (StorageException e) { assertEquals(e.getErrorCode(), "ParentNotFound"); assertEquals(e.getHttpStatusCode(), 404); assertEquals(e.getMessage(), "The specified parent path does not exist."); } // File creation directly in the share should pass. CloudFile file2 = this.share.getRootDirectoryReference().getFileReference("file2"); file2.create(0); dir.create(); file.create(0); }
/** * Ensures we cannot create a directory without creating its parent directory first. * * @throws URISyntaxException * @throws StorageException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class, CloudTests.class }) public void testCloudFileDirectoryCreateDirectoryWithoutParent() throws URISyntaxException, StorageException { CloudFileDirectory dir1 = this.share.getRootDirectoryReference().getDirectoryReference("Dir1"); CloudFileDirectory dir2 = this.share.getRootDirectoryReference().getDirectoryReference("Dir1/Dir2"); try { dir2.create(); fail("Directory shouldn't be created when the parent directory wasn't created."); } catch (StorageException e) { assertEquals(e.getErrorCode(), "ParentNotFound"); assertEquals(e.getHttpStatusCode(), 404); assertEquals(e.getMessage(), "The specified parent path does not exist."); } dir1.create(); dir2.create(); }