Better error message when LocalDataSegmentPusher cannot create its directory.

This commit is contained in:
Gian Merlino 2015-11-10 19:47:37 -08:00
parent e75c2a407d
commit f2c271e500
2 changed files with 67 additions and 47 deletions

View File

@ -78,7 +78,9 @@ public class LocalDataSegmentPusher implements DataSegmentPusher
); );
} }
outDir.mkdirs(); if (!outDir.mkdirs() && !outDir.isDirectory()) {
throw new IOException(String.format("Cannot create directory[%s]", outDir));
}
File outFile = new File(outDir, "index.zip"); File outFile = new File(outDir, "index.zip");
log.info("Compressing files from[%s] to [%s]", dataSegmentFile, outFile); log.info("Compressing files from[%s] to [%s]", dataSegmentFile, outFile);
long size = CompressionUtils.zip(dataSegmentFile, outFile); long size = CompressionUtils.zip(dataSegmentFile, outFile);

View File

@ -20,74 +20,103 @@
package io.druid.segment.loading; package io.druid.segment.loading;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.ByteStreams; import com.google.common.collect.ImmutableList;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
import io.druid.timeline.DataSegment; import io.druid.timeline.DataSegment;
import io.druid.timeline.partition.NoneShardSpec; import io.druid.timeline.partition.NoneShardSpec;
import org.apache.commons.io.FileUtils;
import org.joda.time.Interval; import org.joda.time.Interval;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
public class LocalDataSegmentPusherTest public class LocalDataSegmentPusherTest
{ {
DataSegment dataSegment; @Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public ExpectedException exception = ExpectedException.none();
LocalDataSegmentPusher localDataSegmentPusher; LocalDataSegmentPusher localDataSegmentPusher;
LocalDataSegmentPusherConfig config;
File dataSegmentFiles; File dataSegmentFiles;
File outDir; DataSegment dataSegment = new DataSegment(
"ds",
new Interval(0, 1),
"v1",
null,
null,
null,
new NoneShardSpec(),
null,
-1
);
@Before @Before
public void setUp() throws IOException public void setUp() throws IOException
{ {
dataSegment = new DataSegment( config = new LocalDataSegmentPusherConfig();
"", config.storageDirectory = temporaryFolder.newFolder();
new Interval(0, 1), localDataSegmentPusher = new LocalDataSegmentPusher(config, new ObjectMapper());
"", dataSegmentFiles = temporaryFolder.newFolder();
null, Files.asByteSink(new File(dataSegmentFiles, "version.bin")).write(Ints.toByteArray(0x9));
null,
null,
new NoneShardSpec(),
null,
-1
);
localDataSegmentPusher = new LocalDataSegmentPusher(new LocalDataSegmentPusherConfig(), new ObjectMapper());
dataSegmentFiles = Files.createTempDir();
ByteStreams.write(
Ints.toByteArray(0x9),
Files.newOutputStreamSupplier(new File(dataSegmentFiles, "version.bin"))
);
} }
@Test @Test
public void testPush() throws IOException public void testPush() throws IOException
{ {
/* DataSegment segment - Used to create LoadSpec and Create outDir (Local Deep Storage location in this case) /* DataSegment - Used to create LoadSpec and Create outDir (Local Deep Storage location in this case)
File dataSegmentFile - Used to get location of segment files like version.bin, meta.smoosh and xxxxx.smoosh File dataSegmentFile - Used to get location of segment files like version.bin, meta.smoosh and xxxxx.smoosh
*/ */
DataSegment returnSegment = localDataSegmentPusher.push(dataSegmentFiles, dataSegment); final DataSegment dataSegment2 = dataSegment.withVersion("v2");
Assert.assertNotNull(returnSegment);
Assert.assertEquals(dataSegment, returnSegment); DataSegment returnSegment1 = localDataSegmentPusher.push(dataSegmentFiles, dataSegment);
outDir = new File( DataSegment returnSegment2 = localDataSegmentPusher.push(dataSegmentFiles, dataSegment2);
new LocalDataSegmentPusherConfig().getStorageDirectory(),
DataSegmentPusherUtil.getStorageDir(returnSegment) Assert.assertNotNull(returnSegment1);
Assert.assertEquals(dataSegment, returnSegment1);
Assert.assertNotNull(returnSegment2);
Assert.assertEquals(dataSegment2, returnSegment2);
Assert.assertNotEquals(
DataSegmentPusherUtil.getStorageDir(dataSegment),
DataSegmentPusherUtil.getStorageDir(dataSegment2)
); );
File versionFile = new File(outDir, "index.zip");
File descriptorJson = new File(outDir, "descriptor.json"); for (DataSegment returnSegment : ImmutableList.of(returnSegment1, returnSegment2)) {
Assert.assertTrue(versionFile.exists()); File outDir = new File(
Assert.assertTrue(descriptorJson.exists()); config.getStorageDirectory(),
DataSegmentPusherUtil.getStorageDir(returnSegment)
);
File versionFile = new File(outDir, "index.zip");
File descriptorJson = new File(outDir, "descriptor.json");
Assert.assertTrue(versionFile.exists());
Assert.assertTrue(descriptorJson.exists());
}
}
@Test
public void testPushCannotCreateDirectory() throws IOException
{
exception.expect(IOException.class);
exception.expectMessage("Cannot create directory");
config.storageDirectory = new File(config.storageDirectory, "xxx");
Assert.assertTrue(config.storageDirectory.mkdir());
config.storageDirectory.setWritable(false);
localDataSegmentPusher.push(dataSegmentFiles, dataSegment);
} }
@Test @Test
public void testPathForHadoopAbsolute() public void testPathForHadoopAbsolute()
{ {
LocalDataSegmentPusherConfig config = new LocalDataSegmentPusherConfig();
config.storageDirectory = new File("/druid"); config.storageDirectory = new File("/druid");
Assert.assertEquals( Assert.assertEquals(
@ -99,7 +128,6 @@ public class LocalDataSegmentPusherTest
@Test @Test
public void testPathForHadoopRelative() public void testPathForHadoopRelative()
{ {
LocalDataSegmentPusherConfig config = new LocalDataSegmentPusherConfig();
config.storageDirectory = new File("druid"); config.storageDirectory = new File("druid");
Assert.assertEquals( Assert.assertEquals(
@ -107,14 +135,4 @@ public class LocalDataSegmentPusherTest
new LocalDataSegmentPusher(config, new ObjectMapper()).getPathForHadoop("foo") new LocalDataSegmentPusher(config, new ObjectMapper()).getPathForHadoop("foo")
); );
} }
@After
public void tearDown() throws IOException
{
FileUtils.deleteDirectory(dataSegmentFiles);
if (outDir != null) {
FileUtils.deleteDirectory(outDir);
}
}
} }