This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from jvfullam/issue#18
Added test for Issue #18 that verifies each partition gets its own PUD
- Loading branch information
Showing
5 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
...jbatch.tck/src/main/java/com/ibm/jbatch/tck/artifacts/specialized/PUDPartitionReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2016 International Business Machines Corp. | ||
* | ||
* See the NOTICE file distributed with this work for additional information | ||
* regarding copyright ownership. Licensed under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.ibm.jbatch.tck.artifacts.specialized; | ||
|
||
import java.io.Serializable; | ||
|
||
import javax.batch.api.BatchProperty; | ||
import javax.batch.api.chunk.AbstractItemReader; | ||
import javax.batch.runtime.context.JobContext; | ||
import javax.batch.runtime.context.StepContext; | ||
import javax.inject.Inject; | ||
|
||
import com.ibm.jbatch.tck.artifacts.basicchunk.BasicItem; | ||
|
||
/*NOTE: Code for this class is taken substantially from basicchunk.BasicReader*/ | ||
@javax.inject.Named("PUDPartitionReader") | ||
public class PUDPartitionReader extends AbstractItemReader { | ||
|
||
@Inject | ||
JobContext jobCtx; | ||
|
||
@Inject | ||
StepContext stepCtx; | ||
|
||
@Inject @BatchProperty(name="partition.number") | ||
String partitionNumber; | ||
|
||
@Inject @BatchProperty(name="execution.number") | ||
String executionNumber; | ||
|
||
@Inject | ||
@BatchProperty(name = "number.of.items.to.be.read") | ||
String injectedNumberOfItemsToBeRead; | ||
//Default: read 10 items | ||
private int numberOfItemsToBeRead = 10; | ||
|
||
@Inject | ||
@BatchProperty(name = "throw.reader.exception.for.these.items") | ||
String injectedThrowReaderExceptionForTheseItems; | ||
//Default: don't throw any exceptions | ||
private int[] throwReaderExceptionForTheseItems = {}; | ||
|
||
private String PUDString; | ||
private int currentItemId = -1; | ||
private BasicItem currentItem = null; | ||
|
||
@Override | ||
public void open(Serializable checkpoint) { | ||
|
||
PUDString = "PUD for Partition: " +partitionNumber; | ||
|
||
// Set on the first execution; on later executions it will need to be obtained | ||
// from the job repository's persistent store. | ||
if (checkpoint == null) { | ||
stepCtx.setPersistentUserData(PUDString); | ||
} | ||
|
||
if (injectedNumberOfItemsToBeRead != null) { | ||
numberOfItemsToBeRead = Integer.parseInt(injectedNumberOfItemsToBeRead); | ||
} | ||
|
||
if (injectedThrowReaderExceptionForTheseItems != null) { | ||
String[] exceptionsStringArray = injectedThrowReaderExceptionForTheseItems.split(","); | ||
throwReaderExceptionForTheseItems = new int[exceptionsStringArray.length]; | ||
for (int i = 0; i < exceptionsStringArray.length; i++) { | ||
throwReaderExceptionForTheseItems[i] = Integer.parseInt(exceptionsStringArray[i]); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public BasicItem readItem() throws Exception { | ||
|
||
if (executionNumber.equals("2")) { | ||
if (!stepCtx.getPersistentUserData().equals(PUDString)) { | ||
throw new Exception("BadPersistentUserData: PUD for partition "+partitionNumber+" is expected to be "+PUDString+", but found "+stepCtx.getPersistentUserData()); | ||
} | ||
} | ||
|
||
//Code below is take from BasicReader | ||
|
||
/* Note that BasicReader has no concept of rolling back after a retryable exception is thrown. | ||
* Example: chunk size is 2, we plan to read 10 items (#0-#9), but a retryable exception is thrown while reading item #1 | ||
* In this case, the reader goes on to read #2 when it should be rolling back to #0, and so the writer will never receive | ||
* #0, even though it was previously read successfully */ | ||
|
||
currentItemId++; | ||
|
||
if (currentItemId < numberOfItemsToBeRead) { | ||
currentItem = new BasicItem(currentItemId); | ||
if (readerExceptionShouldBeThrownForCurrentItem()) { | ||
//set the job exit status so we can determine which exception was last thrown | ||
jobCtx.setExitStatus("Exception:Item#" + currentItem.getId()); | ||
throw new Exception("Exception thrown for item " + currentItem.getId()); | ||
} | ||
currentItem.setRead(true); | ||
return currentItem; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private boolean readerExceptionShouldBeThrownForCurrentItem() { | ||
|
||
for (int i: throwReaderExceptionForTheseItems) { | ||
if (currentItem.getId()==i) { return true; } | ||
} | ||
|
||
return false; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...batch.tck/src/main/java/com/ibm/jbatch/tck/artifacts/specialized/PUDPartitionReducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright 2016 International Business Machines Corp. | ||
* | ||
* See the NOTICE file distributed with this work for additional information | ||
* regarding copyright ownership. Licensed under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.ibm.jbatch.tck.artifacts.specialized; | ||
|
||
import javax.batch.api.partition.AbstractPartitionReducer; | ||
import javax.batch.runtime.context.StepContext; | ||
import javax.inject.Inject; | ||
|
||
@javax.inject.Named("PUDPartitionReducer") | ||
public class PUDPartitionReducer extends AbstractPartitionReducer { | ||
|
||
@Inject | ||
StepContext stepCtx; | ||
|
||
public final String TOP_LEVEL_PUD = "This is the Persistent User Data for the top-level stepCtx of the partitioned step!"; | ||
|
||
@Override | ||
public void beginPartitionedStep() throws Exception { | ||
stepCtx.setPersistentUserData(TOP_LEVEL_PUD); | ||
} | ||
|
||
@Override | ||
public void afterPartitionedStepCompletion(PartitionStatus status) throws Exception { | ||
if (!stepCtx.getPersistentUserData().equals(TOP_LEVEL_PUD)) { | ||
throw new Exception("Unexpected PUD at the top level of the Partitioned Step!"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...atch.tck/src/main/resources/META-INF/batch-jobs/partitioned_step_persistent_user_data.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- Copyright 2016 International Business Machines Corp. See the NOTICE file distributed with this work | ||
for additional information regarding copyright ownership. Licensed under the Apache License, Version | ||
2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain | ||
a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law | ||
or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. --> | ||
<job id="partitioned_step_persistent_user_data" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0"> | ||
<step id="step1"> | ||
<chunk item-count="1"> | ||
<reader ref="PUDPartitionReader"> | ||
<properties> | ||
<property name="execution.number" value="#{jobParameters['execution.number']}"/> | ||
<property name="number.of.items.to.be.read" value="#{jobParameters['number.of.items.to.be.read']}"/> | ||
<property name="throw.reader.exception.for.these.items" value="#{jobParameters['throw.reader.exception.for.these.items']}"/> | ||
<property name="partition.number" value="#{partitionPlan['partition.number']}"/> | ||
</properties> | ||
</reader> | ||
<processor ref="basicProcessor"/> | ||
<writer ref="basicWriter"/> | ||
</chunk> | ||
<partition> | ||
<plan partitions="2"> | ||
<properties partition="0"> | ||
<property name="partition.number" value="0"/> | ||
</properties> | ||
<properties partition="1"> | ||
<property name="partition.number" value="1"/> | ||
</properties> | ||
</plan> | ||
<reducer ref="PUDPartitionReducer"/> | ||
</partition> | ||
</step> | ||
</job> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters