|
16 | 16 |
|
17 | 17 | package io.cdap.plugin.batch.action; |
18 | 18 |
|
| 19 | +import com.google.api.client.googleapis.json.GoogleJsonResponseException; |
19 | 20 | import com.google.common.annotations.VisibleForTesting; |
20 | 21 | import io.cdap.cdap.api.annotation.Description; |
21 | 22 | import io.cdap.cdap.api.annotation.Macro; |
@@ -70,7 +71,22 @@ public void run(ActionContext context) throws Exception { |
70 | 71 | Path dest = new Path(config.destPath); |
71 | 72 |
|
72 | 73 | FileSystem fileSystem = source.getFileSystem(new Configuration()); |
73 | | - fileSystem.mkdirs(dest.getParent()); |
| 74 | + try { |
| 75 | + fileSystem.mkdirs(dest.getParent()); |
| 76 | + LOG.debug("mkdirs(): ensured destination parent directory exists: {}", dest.getParent()); |
| 77 | + } catch (GoogleJsonResponseException e) { |
| 78 | + if (e.getStatusCode() == 412 || (e.getStatusMessage() != null && e.getStatusMessage() |
| 79 | + .contains("412 Precondition Failed"))) { |
| 80 | + // Expected GCS directory marker race condition — safe to ignore |
| 81 | + LOG.info("Directory already exists in GCS, skipping creation: {}", dest.getParent()); |
| 82 | + } else { |
| 83 | + String errorReason = String.format("Failed to create parent directory for dest path: %s, Status Code: %s, " + |
| 84 | + "Message: %s", dest.getParent(), e.getStatusCode(), e.getMessage()); |
| 85 | + throw new IOException(errorReason, e); |
| 86 | + } |
| 87 | + } catch (IOException e) { |
| 88 | + throw e; |
| 89 | + } |
74 | 90 |
|
75 | 91 | if (fileSystem.getFileStatus(source).isFile()) { //moving single file |
76 | 92 |
|
@@ -119,7 +135,22 @@ public boolean accept(Path path) { |
119 | 135 | throw new IllegalArgumentException(String.format("destPath %s needs to be a directory since sourcePath is a " + |
120 | 136 | "directory", config.destPath)); |
121 | 137 | } |
122 | | - fileSystem.mkdirs(dest); //create destination directory if necessary |
| 138 | + try { |
| 139 | + fileSystem.mkdirs(dest); //create destination directory if necessary |
| 140 | + LOG.debug("mkdirs(): ensured destination parent directory exists: {}", dest.getParent()); |
| 141 | + } catch (GoogleJsonResponseException e) { |
| 142 | + if (e.getStatusCode() == 412 || (e.getStatusMessage() != null && e.getStatusMessage() |
| 143 | + .contains("412 Precondition Failed"))) { |
| 144 | + // Expected GCS directory marker race condition — safe to ignore |
| 145 | + LOG.info("Directory already exists in GCS, skipping creation: {}", dest.getParent()); |
| 146 | + } else { |
| 147 | + String errorReason = String.format("Failed to create parent directory for dest path: %s, Status Code: %s, " + |
| 148 | + "Message: %s", dest.getParent(), e.getStatusCode(), e.getMessage()); |
| 149 | + throw new IOException(errorReason, e); |
| 150 | + } |
| 151 | + } catch (IOException e) { |
| 152 | + throw e; |
| 153 | + } |
123 | 154 |
|
124 | 155 | for (FileStatus file : listFiles) { |
125 | 156 | source = file.getPath(); |
|
0 commit comments