shaofengshi closed pull request #200: KYLIN-3449 Allow deleting a NEW status 
orphan segment
URL: https://github.com/apache/kylin/pull/200
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/server-base/src/main/java/org/apache/kylin/rest/service/CubeService.java 
b/server-base/src/main/java/org/apache/kylin/rest/service/CubeService.java
index 8608bafc41..5e2c49efcd 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/service/CubeService.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/service/CubeService.java
@@ -42,7 +42,10 @@
 import org.apache.kylin.engine.EngineFactory;
 import org.apache.kylin.engine.mr.CubingJob;
 import org.apache.kylin.engine.mr.common.CuboidRecommenderUtil;
+import org.apache.kylin.job.JobInstance;
 import org.apache.kylin.job.common.PatternedLogger;
+import org.apache.kylin.job.constant.JobStatusEnum;
+import org.apache.kylin.job.constant.JobTimeFilterEnum;
 import org.apache.kylin.job.exception.JobException;
 import org.apache.kylin.job.execution.DefaultChainedExecutable;
 import org.apache.kylin.job.execution.ExecutableState;
@@ -510,7 +513,13 @@ public CubeInstance deleteSegment(CubeInstance cube, 
String segmentName) throws
         }
 
         if (toDelete.getStatus() != SegmentStatusEnum.READY) {
-            throw new 
BadRequestException(String.format(msg.getDELETE_NOT_READY_SEG(), segmentName));
+            if (toDelete.getStatus() == SegmentStatusEnum.NEW) {
+                if (!isOrphonSegment(cube, toDelete.getUuid())) {
+                    throw new 
BadRequestException(String.format(msg.getDELETE_NOT_READY_SEG(), segmentName));
+                }
+            } else {
+                throw new 
BadRequestException(String.format(msg.getDELETE_NOT_READY_SEG(), segmentName));
+            }
         }
 
         if (!segmentName.equals(cube.getSegments().get(0).getName())
@@ -521,6 +530,18 @@ public CubeInstance deleteSegment(CubeInstance cube, 
String segmentName) throws
         return 
CubeManager.getInstance(getConfig()).updateCubeDropSegments(cube, toDelete);
     }
 
+    public boolean isOrphonSegment(CubeInstance cube, String segId) {
+        List<JobInstance> jobInstances = 
jobService.searchJobsByCubeName(cube.getName(), cube.getProject(), 
Lists.newArrayList(JobStatusEnum.NEW, JobStatusEnum.PENDING, 
JobStatusEnum.RUNNING, JobStatusEnum.ERROR, JobStatusEnum.STOPPED),
+                JobTimeFilterEnum.ALL, JobService.JobSearchMode.CUBING_ONLY);
+        for (JobInstance jobInstance : jobInstances) {
+            // if there are segment related jobs, can not delete this segment.
+            if (segId.equals(jobInstance.getRelatedSegment())) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     protected void releaseAllJobs(CubeInstance cube) {
         final List<CubingJob> cubingJobs = 
jobService.listJobsByRealizationName(cube.getName(), null);
         for (CubingJob cubingJob : cubingJobs) {
diff --git 
a/server/src/test/java/org/apache/kylin/rest/controller/CubeControllerTest.java 
b/server/src/test/java/org/apache/kylin/rest/controller/CubeControllerTest.java
index 6ebd9e825d..d30391b2e0 100644
--- 
a/server/src/test/java/org/apache/kylin/rest/controller/CubeControllerTest.java
+++ 
b/server/src/test/java/org/apache/kylin/rest/controller/CubeControllerTest.java
@@ -138,7 +138,26 @@ public void testDeleteSegmentNew() throws IOException {
         CubeDesc[] cubes = cubeDescController.getCube(cubeName);
         Assert.assertNotNull(cubes);
 
-        cubeController.deleteSegment(cubeName, 
"20131212000000_20140112000000");
+        String segmentName = "20131212000000_20140112000000";
+
+        CubeInstance cube = cubeService.getCubeManager().getCube(cubeName);
+        CubeSegment toDelete = null;
+        for (CubeSegment seg : cube.getSegments()) {
+            if (seg.getName().equals(segmentName)) {
+                toDelete = seg;
+                break;
+            }
+        }
+
+        Assert.assertNotNull(toDelete);
+        String segId = toDelete.getUuid();
+
+        cubeController.deleteSegment(cubeName, segmentName);
+
+        // delete success, no related job 'NEW' segment can be delete
+        if (cubeService.isOrphonSegment(cube, segId)){
+            throw new InternalErrorException();
+        }
     }
 
     @Test(expected = NotFoundException.class)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to