|
49 | 49 | import org.elasticsearch.common.xcontent.XContentFactory; |
50 | 50 | import org.elasticsearch.common.xcontent.XContentLocation; |
51 | 51 | import org.elasticsearch.discovery.DiscoverySettings; |
| 52 | +import org.elasticsearch.env.ShardLockObtainFailedException; |
52 | 53 | import org.elasticsearch.index.AlreadyExpiredException; |
53 | 54 | import org.elasticsearch.index.Index; |
54 | 55 | import org.elasticsearch.index.engine.RecoveryEngineException; |
|
107 | 108 | import static java.util.Collections.emptySet; |
108 | 109 | import static java.util.Collections.singleton; |
109 | 110 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 111 | +import static org.hamcrest.Matchers.instanceOf; |
110 | 112 |
|
111 | 113 | public class ExceptionSerializationTests extends ESTestCase { |
112 | 114 |
|
@@ -160,10 +162,10 @@ private void checkClass(Class<?> clazz) { |
160 | 162 | if (isEsException(clazz) == false) { |
161 | 163 | return; |
162 | 164 | } |
163 | | - if (ElasticsearchException.isRegistered(clazz.asSubclass(Throwable.class)) == false |
| 165 | + if (ElasticsearchException.isRegistered(clazz.asSubclass(Throwable.class), Version.CURRENT) == false |
164 | 166 | && ElasticsearchException.class.equals(clazz.getEnclosingClass()) == false) { |
165 | 167 | notRegistered.add(clazz); |
166 | | - } else if (ElasticsearchException.isRegistered(clazz.asSubclass(Throwable.class))) { |
| 168 | + } else if (ElasticsearchException.isRegistered(clazz.asSubclass(Throwable.class), Version.CURRENT)) { |
167 | 169 | registered.add(clazz); |
168 | 170 | try { |
169 | 171 | if (clazz.getMethod("writeTo", StreamOutput.class) != null) { |
@@ -218,10 +220,17 @@ public TestException(StreamInput in) throws IOException { |
218 | 220 | } |
219 | 221 |
|
220 | 222 | private <T extends Exception> T serialize(T exception) throws IOException { |
221 | | - ElasticsearchAssertions.assertVersionSerializable(VersionUtils.randomVersion(random()), exception); |
| 223 | + return serialize(exception, VersionUtils.randomVersion(random())); |
| 224 | + } |
| 225 | + |
| 226 | + private <T extends Exception> T serialize(T exception, Version version) throws IOException { |
| 227 | + ElasticsearchAssertions.assertVersionSerializable(version, exception); |
222 | 228 | BytesStreamOutput out = new BytesStreamOutput(); |
| 229 | + out.setVersion(version); |
223 | 230 | out.writeException(exception); |
| 231 | + |
224 | 232 | StreamInput in = out.bytes().streamInput(); |
| 233 | + in.setVersion(version); |
225 | 234 | return in.readException(); |
226 | 235 | } |
227 | 236 |
|
@@ -769,6 +778,7 @@ public void testIds() { |
769 | 778 | ids.put(144, org.elasticsearch.cluster.NotMasterException.class); |
770 | 779 | ids.put(145, org.elasticsearch.ElasticsearchStatusException.class); |
771 | 780 | ids.put(146, org.elasticsearch.tasks.TaskCancelledException.class); |
| 781 | + ids.put(147, org.elasticsearch.env.ShardLockObtainFailedException.class); |
772 | 782 |
|
773 | 783 | Map<Class<? extends ElasticsearchException>, Integer> reverse = new HashMap<>(); |
774 | 784 | for (Map.Entry<Integer, Class<? extends ElasticsearchException>> entry : ids.entrySet()) { |
@@ -826,4 +836,28 @@ public void testElasticsearchRemoteException() throws IOException { |
826 | 836 | assertEquals(ex.status(), e.status()); |
827 | 837 | assertEquals(RestStatus.TOO_MANY_REQUESTS, e.status()); |
828 | 838 | } |
| 839 | + |
| 840 | + public void testShardLockObtainFailedException() throws IOException { |
| 841 | + ShardId shardId = new ShardId("foo", "_na_", 1); |
| 842 | + ShardLockObtainFailedException orig = new ShardLockObtainFailedException(shardId, "boom"); |
| 843 | + Version version = VersionUtils.randomVersionBetween(random(), |
| 844 | + Version.V_5_0_0, Version.CURRENT); |
| 845 | + if (version.before(ElasticsearchException.V_5_1_0_UNRELEASED)) { |
| 846 | + // remove this once 5_1_0 is released randomVersionBetween asserts that this version is in the constant table.. |
| 847 | + version = ElasticsearchException.V_5_1_0_UNRELEASED; |
| 848 | + } |
| 849 | + ShardLockObtainFailedException ex = serialize(orig, version); |
| 850 | + assertEquals(orig.getMessage(), ex.getMessage()); |
| 851 | + assertEquals(orig.getShardId(), ex.getShardId()); |
| 852 | + } |
| 853 | + |
| 854 | + public void testBWCShardLockObtainFailedException() throws IOException { |
| 855 | + ShardId shardId = new ShardId("foo", "_na_", 1); |
| 856 | + ShardLockObtainFailedException orig = new ShardLockObtainFailedException(shardId, "boom"); |
| 857 | + Exception ex = serialize((Exception)orig, Version.V_5_0_0); |
| 858 | + assertThat(ex, instanceOf(NotSerializableExceptionWrapper.class)); |
| 859 | + assertEquals("shard_lock_obtain_failed_exception: [foo][1]: boom", ex.getMessage()); |
| 860 | + } |
| 861 | + |
| 862 | + |
829 | 863 | } |
0 commit comments