|
35 | 35 | import org.apache.hadoop.fs.permission.FsPermission; |
36 | 36 | import org.apache.hadoop.test.GenericTestUtils; |
37 | 37 |
|
| 38 | +import org.apache.hadoop.fs.azurebfs.contracts.exceptions.AbfsRestOperationException; |
| 39 | +import org.apache.hadoop.fs.azurebfs.contracts.exceptions.ConcurrentWriteOperationDetectedException; |
| 40 | +import org.apache.hadoop.fs.azurebfs.services.AbfsHttpOperation; |
| 41 | +import org.apache.hadoop.fs.azurebfs.services.AbfsRestOperation; |
| 42 | +import org.apache.hadoop.fs.azurebfs.services.AbfsUriQueryBuilder; |
| 43 | + |
| 44 | +import static java.net.HttpURLConnection.HTTP_CONFLICT; |
| 45 | +import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; |
| 46 | +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; |
| 47 | +import static java.net.HttpURLConnection.HTTP_OK; |
| 48 | +import static java.net.HttpURLConnection.HTTP_PRECON_FAILED; |
| 49 | + |
| 50 | +import static org.mockito.ArgumentMatchers.any; |
| 51 | +import static org.mockito.ArgumentMatchers.eq; |
| 52 | +import static org.mockito.Mockito.doThrow; |
| 53 | +import static org.mockito.Mockito.mock; |
| 54 | +import static org.mockito.Mockito.when; |
| 55 | + |
38 | 56 | import static org.apache.hadoop.fs.contract.ContractTestUtils.assertIsFile; |
39 | 57 | import static org.apache.hadoop.test.LambdaTestUtils.intercept; |
40 | | - |
41 | 58 | import static org.apache.hadoop.fs.azurebfs.AbfsStatistic.CONNECTIONS_MADE; |
42 | 59 |
|
43 | 60 | /** |
@@ -286,4 +303,151 @@ public void testCreateFileOverwrite(boolean defaultDisableCreateOverwrite) |
286 | 303 | totalConnectionMadeBeforeTest + createRequestCount, |
287 | 304 | fs.getInstrumentationMap()); |
288 | 305 | } |
| 306 | + |
| 307 | + /** |
| 308 | + * Test negative scenarios with Create overwrite=false as default |
| 309 | + * With create overwrite=true ending in 3 calls: |
| 310 | + * A. Create overwrite=false |
| 311 | + * B. GFS |
| 312 | + * C. Create overwrite=true |
| 313 | + * |
| 314 | + * Scn1: A fails with HTTP409, leading to B which fails with HTTP404, |
| 315 | + * detect parallel access |
| 316 | + * Scn2: A fails with HTTP409, leading to B which fails with HTTP500, |
| 317 | + * fail create with HTTP500 |
| 318 | + * Scn3: A fails with HTTP409, leading to B and then C, |
| 319 | + * which fails with HTTP412, detect parallel access |
| 320 | + * Scn4: A fails with HTTP409, leading to B and then C, |
| 321 | + * which fails with HTTP500, fail create with HTTP500 |
| 322 | + * Scn5: A fails with HTTP500, fail create with HTTP500 |
| 323 | + */ |
| 324 | + @Test |
| 325 | + public void testNegativeScenariosForCreateOverwriteDisabled() |
| 326 | + throws Throwable { |
| 327 | + |
| 328 | + final AzureBlobFileSystem currentFs = getFileSystem(); |
| 329 | + Configuration config = new Configuration(this.getRawConfiguration()); |
| 330 | + config.set("fs.azure.disable.default.create.overwrite", |
| 331 | + Boolean.toString(true)); |
| 332 | + |
| 333 | + final AzureBlobFileSystem fs = |
| 334 | + (AzureBlobFileSystem) FileSystem.newInstance(currentFs.getUri(), |
| 335 | + config); |
| 336 | + |
| 337 | + // Get mock AbfsClient with current config |
| 338 | + org.apache.hadoop.fs.azurebfs.services.AbfsClient |
| 339 | + mockClient |
| 340 | + = org.apache.hadoop.fs.azurebfs.services.TestAbfsClient.getMockAbfsClient( |
| 341 | + fs.getAbfsStore().getClient(), |
| 342 | + fs.getAbfsStore().getAbfsConfiguration()); |
| 343 | + |
| 344 | + AbfsRestOperation successOp = mock( |
| 345 | + AbfsRestOperation.class); |
| 346 | + AbfsHttpOperation http200Op = mock( |
| 347 | + AbfsHttpOperation.class); |
| 348 | + when(http200Op.getStatusCode()).thenReturn(HTTP_OK); |
| 349 | + when(successOp.getResult()).thenReturn(http200Op); |
| 350 | + |
| 351 | + AbfsRestOperationException conflictResponseEx |
| 352 | + = getMockAbfsRestOperationException(HTTP_CONFLICT); |
| 353 | + AbfsRestOperationException serverErrorResponseEx |
| 354 | + = getMockAbfsRestOperationException(HTTP_INTERNAL_ERROR); |
| 355 | + AbfsRestOperationException fileNotFoundResponseEx |
| 356 | + = getMockAbfsRestOperationException(HTTP_NOT_FOUND); |
| 357 | + AbfsRestOperationException preConditionResponseEx |
| 358 | + = getMockAbfsRestOperationException(HTTP_PRECON_FAILED); |
| 359 | + |
| 360 | + doThrow(conflictResponseEx) // Scn1: GFS fails with Http404 |
| 361 | + .doThrow(conflictResponseEx) // Scn2: GFS fails with Http500 |
| 362 | + .doThrow( |
| 363 | + conflictResponseEx) // Scn3: create overwrite=true fails with Http412 |
| 364 | + .doThrow( |
| 365 | + conflictResponseEx) // Scn4: create overwrite=true fails with Http500 |
| 366 | + .doThrow( |
| 367 | + serverErrorResponseEx) // Scn5: create overwrite=false fails with Http500 |
| 368 | + .when(mockClient) |
| 369 | + .createPathImpl(any(String.class), any( |
| 370 | + AbfsUriQueryBuilder.class), |
| 371 | + eq(false), any(String.class), any(String.class), eq(null)); |
| 372 | + |
| 373 | + doThrow(fileNotFoundResponseEx) // Scn1: GFS fails with Http404 |
| 374 | + .doThrow(serverErrorResponseEx) // Scn2: GFS fails with Http500 |
| 375 | + .doReturn(successOp) // Scn3: create overwrite=true fails with Http412 |
| 376 | + .doReturn(successOp) // Scn4: create overwrite=true fails with Http500 |
| 377 | + .when(mockClient) |
| 378 | + .getPathStatus(any(String.class), eq(false)); |
| 379 | + |
| 380 | + doThrow( |
| 381 | + preConditionResponseEx) // Scn3: create overwrite=true fails with Http412 |
| 382 | + .doThrow( |
| 383 | + serverErrorResponseEx) // Scn4: create overwrite=true fails with Http500 |
| 384 | + .when(mockClient) |
| 385 | + .createPathImpl(any(String.class), any( |
| 386 | + AbfsUriQueryBuilder.class), |
| 387 | + eq(true), any(String.class), any(String.class), eq(null)); |
| 388 | + |
| 389 | + when(mockClient.createPath(any(String.class), eq(true), eq(true), |
| 390 | + any(String.class), |
| 391 | + any(String.class), eq(false))).thenCallRealMethod(); |
| 392 | + |
| 393 | + // Scn1: GFS fails with Http404 |
| 394 | + // Sequence of events expected: |
| 395 | + // 1. create overwrite=false - fail with conflict |
| 396 | + // 2. GFS - fail with File Not found |
| 397 | + // Create will fail with ConcurrentWriteOperationDetectedException |
| 398 | + intercept( |
| 399 | + ConcurrentWriteOperationDetectedException.class, |
| 400 | + () -> |
| 401 | + mockClient.createPath("someTestPath", true, true, "0644", "0022", |
| 402 | + false)); |
| 403 | + |
| 404 | + // Scn2: GFS fails with Http500 |
| 405 | + // Sequence of events expected: |
| 406 | + // 1. create overwrite=false - fail with conflict |
| 407 | + // 2. GFS - fail with Server error |
| 408 | + // Create will fail with 500 |
| 409 | + intercept( |
| 410 | + AbfsRestOperationException.class, |
| 411 | + () -> |
| 412 | + mockClient.createPath("someTestPath", true, true, "0644", "0022", |
| 413 | + false)); |
| 414 | + |
| 415 | + // Scn3: create overwrite=true fails with Http412 |
| 416 | + // Sequence of events expected: |
| 417 | + // 1. create overwrite=false - fail with conflict |
| 418 | + // 2. GFS - pass |
| 419 | + // 3. create overwrite=true - fail with Pre-Condition |
| 420 | + // Create will fail with ConcurrentWriteOperationDetectedException |
| 421 | + intercept( |
| 422 | + ConcurrentWriteOperationDetectedException.class, |
| 423 | + () -> |
| 424 | + mockClient.createPath("someTestPath", true, true, "0644", "0022", |
| 425 | + false)); |
| 426 | + |
| 427 | + // Scn4: create overwrite=true fails with Http500 |
| 428 | + // Sequence of events expected: |
| 429 | + // 1. create overwrite=false - fail with conflict |
| 430 | + // 2. GFS - pass |
| 431 | + // 3. create overwrite=true - fail with Server error |
| 432 | + // Create will fail with 500 |
| 433 | + intercept( |
| 434 | + AbfsRestOperationException.class, |
| 435 | + () -> |
| 436 | + mockClient.createPath("someTestPath", true, true, "0644", "0022", |
| 437 | + false)); |
| 438 | + |
| 439 | + // Scn5: create overwrite=false fails with Http500 |
| 440 | + // Sequence of events expected: |
| 441 | + // 1. create overwrite=false - fail with server error |
| 442 | + // Create will fail with 500 |
| 443 | + intercept( |
| 444 | + AbfsRestOperationException.class, |
| 445 | + () -> |
| 446 | + mockClient.createPath("someTestPath", true, true, "0644", "0022", |
| 447 | + false)); |
| 448 | + } |
| 449 | + |
| 450 | + private AbfsRestOperationException getMockAbfsRestOperationException(int status) { |
| 451 | + return new AbfsRestOperationException(status, "", "", new Exception()); |
| 452 | + } |
289 | 453 | } |
0 commit comments