17
17
18
18
import com .google .common .collect .ImmutableList ;
19
19
import com .google .common .collect .ImmutableMap ;
20
+ import com .google .common .collect .Iterables ;
20
21
import com .google .common .collect .Lists ;
21
22
import com .spectralogic .ds3client .Ds3Client ;
22
23
import com .spectralogic .ds3client .Ds3ClientBuilder ;
29
30
import com .spectralogic .ds3client .helpers .events .FailureEvent ;
30
31
import com .spectralogic .ds3client .helpers .events .SameThreadEventRunner ;
31
32
import com .spectralogic .ds3client .helpers .options .WriteJobOptions ;
33
+ import com .spectralogic .ds3client .helpers .pagination .GetBucketKeyLoaderFactory ;
32
34
import com .spectralogic .ds3client .helpers .strategy .blobstrategy .*;
33
35
import com .spectralogic .ds3client .helpers .strategy .channelstrategy .ChannelStrategy ;
34
36
import com .spectralogic .ds3client .helpers .strategy .channelstrategy .SequentialFileReaderChannelStrategy ;
43
45
import com .spectralogic .ds3client .utils .ByteArraySeekableByteChannel ;
44
46
import com .spectralogic .ds3client .utils .Platform ;
45
47
import com .spectralogic .ds3client .utils .ResourceUtils ;
48
+ import com .spectralogic .ds3client .utils .collections .LazyIterable ;
46
49
import com .spectralogic .ds3client .utils .hashing .ChecksumUtils ;
47
50
import com .spectralogic .ds3client .utils .hashing .Hasher ;
48
51
import org .apache .commons .io .FileUtils ;
@@ -91,6 +94,11 @@ public class PutJobManagement_Test {
91
94
private static TempStorageIds envStorageIds ;
92
95
private static UUID envDataPolicyId ;
93
96
97
+ private static final String SOURCE_DIRECTORY = "little_files/" ;
98
+ private static final String SOURCE_FILE_BASE_NAME = "tape" ;
99
+ private static final String SOURCE_FILE_EXTENSION = "png" ;
100
+ private static final String SOURCE_FILE_NAME = SOURCE_FILE_BASE_NAME + "." + SOURCE_FILE_EXTENSION ;
101
+
94
102
@ BeforeClass
95
103
public static void startup () throws IOException {
96
104
envDataPolicyId = TempStorageUtil .setupDataPolicy (TEST_ENV_NAME , false , ChecksumType .Type .MD5 , client );
@@ -1946,31 +1954,7 @@ public void testPutting15000Files() throws IOException, URISyntaxException {
1946
1954
Path tempDirectory = Files .createTempDirectory (Paths .get ("." ), tempPathPrefix );
1947
1955
1948
1956
try {
1949
- final String sourceDirectory = "little_files/" ;
1950
- final String sourceFileName = "tape.png" ;
1951
-
1952
- final Path sourceFilePath = ResourceUtils .loadFileResource (sourceDirectory + sourceFileName );
1953
-
1954
- Files .copy (sourceFilePath , Paths .get (tempDirectory .toString (), sourceFileName ));
1955
-
1956
- final List <String > fileNames = new ArrayList <>(15001 );
1957
- fileNames .add (sourceFileName );
1958
-
1959
- for (int i = 1 ; i <= 15000 ; ++i ) {
1960
- final String destinationFileName = "tape" + i + ".png" ;
1961
- fileNames .add (destinationFileName );
1962
- Files .copy (sourceFilePath , Paths .get (tempDirectory .toString (), destinationFileName ));
1963
- }
1964
-
1965
- final List <Ds3Object > ds3Objects = new ArrayList <>();
1966
-
1967
- for (final String fileName : fileNames ) {
1968
- final Path filePath = Paths .get (tempDirectory .toString (), fileName );
1969
- final long fileSize = Files .size (filePath );
1970
- final Ds3Object ds3Object = new Ds3Object (fileName , fileSize );
1971
-
1972
- ds3Objects .add (ds3Object );
1973
- }
1957
+ final ImmutableList <Ds3Object > ds3Objects = copyFilesAndGenerateDsObjects (tempDirectory );
1974
1958
1975
1959
final Ds3ClientHelpers ds3ClientHelpers = Ds3ClientHelpers .wrap (client );
1976
1960
final Ds3ClientHelpers .Job writeJob = ds3ClientHelpers .startWriteJob (BUCKET_NAME , ds3Objects );
@@ -1982,26 +1966,150 @@ public void testPutting15000Files() throws IOException, URISyntaxException {
1982
1966
tempDirectory = Files .createTempDirectory (Paths .get ("." ), tempPathPrefix );
1983
1967
1984
1968
final Ds3ClientHelpers .Job readJob = ds3ClientHelpers .startReadJob (BUCKET_NAME , Lists .newArrayList (
1985
- new Ds3Object (sourceFileName , Files .size (sourceFilePath ))));
1969
+ new Ds3Object (SOURCE_FILE_NAME , Files .size (sourceFilePath () ))));
1986
1970
1987
1971
final GetJobSpectraS3Response jobSpectraS3Response = client .getJobSpectraS3 (new GetJobSpectraS3Request (readJob .getJobId ()));
1988
1972
1989
1973
assertThat (jobSpectraS3Response .getMasterObjectListResult (), is (notNullValue ()));
1990
1974
1991
1975
readJob .transfer (new FileObjectGetter (tempDirectory ));
1992
1976
1993
- final File originalFile = ResourceUtils .loadFileResource (sourceDirectory + sourceFileName ).toFile ();
1994
- final File fileCopiedFromBP = Paths .get (tempDirectory .toString (), sourceFileName ).toFile ();
1977
+ final File originalFile = ResourceUtils .loadFileResource (SOURCE_DIRECTORY + SOURCE_FILE_NAME ).toFile ();
1978
+ final File fileCopiedFromBP = Paths .get (tempDirectory .toString (), SOURCE_FILE_NAME ).toFile ();
1995
1979
assertTrue (FileUtils .contentEquals (originalFile , fileCopiedFromBP ));
1996
1980
} catch (final org .apache .http .client .ClientProtocolException e ) {
1997
- fail ("This test makes sure that we don't run out of connections when transferring lots of small files. Oops" );
1981
+ fail ("This test makes sure that we don't run out of connections when transferring lots of small files. Oops. " );
1998
1982
} finally {
1999
1983
FileUtils .deleteDirectory (tempDirectory .toFile ());
2000
1984
cancelAllJobsForBucket (client , BUCKET_NAME );
2001
1985
deleteAllContents (client , BUCKET_NAME );
2002
1986
}
2003
1987
}
2004
1988
1989
+ private Path sourceFilePath () throws IOException , URISyntaxException {
1990
+ return ResourceUtils .loadFileResource (SOURCE_DIRECTORY + SOURCE_FILE_NAME );
1991
+ }
1992
+
1993
+ private ImmutableList <Ds3Object > copyFilesAndGenerateDsObjects (final Path destinationDirectory ) throws IOException , URISyntaxException {
1994
+ final Path sourceFilePath = ResourceUtils .loadFileResource (SOURCE_DIRECTORY + SOURCE_FILE_NAME );
1995
+
1996
+ final ImmutableList <String > fileNames = generateFileNames (SOURCE_FILE_BASE_NAME , SOURCE_FILE_EXTENSION , 15000 );
1997
+ copyFiles (sourceFilePath , destinationDirectory , fileNames );
1998
+
1999
+ return generateDs3Objects (destinationDirectory , fileNames );
2000
+ }
2001
+
2002
+ private ImmutableList <String > generateFileNames (final String baseFileName , final String baseFileExtension , final int numFileNames ) {
2003
+ final ImmutableList .Builder <String > listBuilder = ImmutableList .builder ();
2004
+
2005
+ listBuilder .add (baseFileName + "." + baseFileExtension );
2006
+
2007
+ for (int i = 1 ; i < numFileNames ; ++i ) {
2008
+ listBuilder .add (baseFileName + i + "." + baseFileExtension );
2009
+ }
2010
+
2011
+ return listBuilder .build ();
2012
+ }
2013
+
2014
+ private void copyFiles (final Path sourceFilePath , final Path destinationDirectory , final ImmutableList <String > fileNames ) throws IOException {
2015
+ for (final String fileName : fileNames ) {
2016
+ Files .copy (sourceFilePath , Paths .get (destinationDirectory .toString (), fileName ));
2017
+ }
2018
+ }
2019
+
2020
+ private ImmutableList <Ds3Object > generateDs3Objects (final Path destinationDirectory , final ImmutableList <String > fileNames ) throws IOException {
2021
+ final ImmutableList .Builder <Ds3Object > listBuilder = ImmutableList .builder ();
2022
+
2023
+ for (final String fileName : fileNames ) {
2024
+ final Path filePath = Paths .get (destinationDirectory .toString (), fileName );
2025
+ final long fileSize = Files .size (filePath );
2026
+ final Ds3Object ds3Object = new Ds3Object (fileName , fileSize );
2027
+
2028
+ listBuilder .add (ds3Object );
2029
+ }
2030
+
2031
+ return listBuilder .build ();
2032
+ }
2033
+
2034
+ @ Test
2035
+ public void testCancelingJob () throws URISyntaxException , InterruptedException {
2036
+ final String tempPathPrefix = null ;
2037
+ Path tempDirectory ;
2038
+
2039
+ try {
2040
+ tempDirectory = Files .createTempDirectory (Paths .get ("." ), tempPathPrefix );
2041
+ } catch (final IOException e ) {
2042
+ fail ("Could not create temp folder." );
2043
+ return ;
2044
+ }
2045
+
2046
+ try {
2047
+ final ImmutableList <Ds3Object > ds3Objects = copyFilesAndGenerateDsObjects (tempDirectory );
2048
+
2049
+ final Ds3ClientHelpers ds3ClientHelpers = Ds3ClientHelpers .wrap (client );
2050
+
2051
+ final Ds3ClientHelpers .Job writeJob = ds3ClientHelpers .startWriteJob (BUCKET_NAME , ds3Objects );
2052
+
2053
+ final AtomicBoolean caughtExceptionWhileCanceling = new AtomicBoolean (false );
2054
+ final AtomicBoolean cancelHandlerCalled = new AtomicBoolean (false );
2055
+ final CountDownLatch countDownLatch = new CountDownLatch (1 );
2056
+
2057
+ final AtomicBoolean hasBeenCancelled = new AtomicBoolean (false );
2058
+ writeJob .attachDataTransferredListener (dataTransferred -> {
2059
+ if (hasBeenCancelled .compareAndSet (false , true )) {
2060
+ new Thread (() -> {
2061
+ try {
2062
+ writeJob .cancel ();
2063
+ } catch (IOException e ) {
2064
+ fail ();
2065
+ } finally {
2066
+ countDownLatch .countDown ();
2067
+ }
2068
+ }).start ();
2069
+ }
2070
+ });
2071
+
2072
+ writeJob .attachCanceledEventObserver (new CanceledEventObserver (eventData -> {
2073
+ cancelHandlerCalled .set (true );
2074
+ assertEquals (writeJob .getJobId (), eventData );
2075
+ }));
2076
+
2077
+ writeJob .transfer (new FileObjectPutter (tempDirectory ));
2078
+
2079
+ countDownLatch .await ();
2080
+
2081
+ assertFalse (caughtExceptionWhileCanceling .get ());
2082
+ assertTrue (cancelHandlerCalled .get ());
2083
+
2084
+ final String prefix = "" ;
2085
+ final String nextMarker = null ;
2086
+ final int maxKeys = 15000 ;
2087
+ final String delimiter = null ;
2088
+ final int numRetries = 5 ;
2089
+
2090
+ final GetBucketKeyLoaderFactory <Contents > getBucketKeyLoaderFactory = new GetBucketKeyLoaderFactory <>(client , BUCKET_NAME , prefix , delimiter , nextMarker , maxKeys , numRetries , GetBucketKeyLoaderFactory .contentsFunction );
2091
+ final LazyIterable <?> iterable = new LazyIterable <>(getBucketKeyLoaderFactory );
2092
+ final int numThingsInBucket = Iterables .size (iterable );
2093
+
2094
+ assertTrue (numThingsInBucket < maxKeys );
2095
+
2096
+ final GetCanceledJobSpectraS3Response getCanceledJobSpectraS3Response = client .getCanceledJobSpectraS3 (new GetCanceledJobSpectraS3Request (writeJob .getJobId ().toString ()));
2097
+ final CanceledJob canceledJob = getCanceledJobSpectraS3Response .getCanceledJobResult ();
2098
+ assertEquals (writeJob .getJobId (), canceledJob .getId ());
2099
+ } catch (final org .apache .http .client .ClientProtocolException e ) {
2100
+ fail ("This test makes sure that we don't run out of connections when transferring lots of small files. Oops." );
2101
+ } catch (final IOException e ) {
2102
+ fail ("IOException from something other than job cancelation." );
2103
+ } finally {
2104
+ try {
2105
+ FileUtils .deleteDirectory (tempDirectory .toFile ());
2106
+ deleteAllContents (client , BUCKET_NAME );
2107
+ } catch (final IOException e ) {
2108
+ LOG .error ("Failure cleaning up." , e );
2109
+ }
2110
+ }
2111
+ }
2112
+
2005
2113
@ Test
2006
2114
public void testThatFifoIsNotProcessed () throws IOException , InterruptedException {
2007
2115
Assume .assumeFalse (Platform .isWindows ());
0 commit comments