forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-8879] [avro] Add concurrency check Avro Serializer on DEBUG le…
…vel.
- Loading branch information
1 parent
57ff6e8
commit be7c895
Showing
6 changed files
with
374 additions
and
13 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
.../src/main/java/org/apache/flink/formats/avro/typeutils/AvroSerializerDebugInitHelper.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,47 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* https://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 org.apache.flink.formats.avro.typeutils; | ||
|
||
import org.apache.flink.annotation.Internal; | ||
|
||
/** | ||
* Simple helper class to initialize the concurrency checks for tests. | ||
* | ||
* <p>The flag is automatically set to true when assertions are activated (tests) | ||
* and can be set to true manually in other tests as well; | ||
*/ | ||
@Internal | ||
class AvroSerializerDebugInitHelper { | ||
|
||
/** This captures the initial setting after initialization. It is used to | ||
* validate in tests that we never change the default to true. */ | ||
static final boolean INITIAL_SETTING; | ||
|
||
/** The flag that is used to initialize the KryoSerializer's concurrency check flag. */ | ||
static boolean setToDebug = false; | ||
|
||
static { | ||
// capture the default setting, for tests | ||
INITIAL_SETTING = setToDebug; | ||
|
||
// if assertions are active, the check should be activated | ||
//noinspection AssertWithSideEffects,ConstantConditions | ||
assert setToDebug = true; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...org/apache/flink/formats/avro/typeutils/AvroSerializerConcurrencyCheckInactiveITCase.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,62 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* https://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 org.apache.flink.formats.avro.typeutils; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* A test that validates that the concurrency checks in the Avro Serializer | ||
* are not hard coded to active. | ||
* | ||
* <p>The debug initialization in the AvroSerializer happens together with class | ||
* initialization (that makes it peak efficient), which is why this test needs to | ||
* run in a fresh JVM fork, and the JVM fork of this test should not be reused. | ||
* | ||
* <p><b>Important:</b> If you see this test fail and the initial settings are still | ||
* correct, check the assumptions above (on fresh JVM fork). | ||
*/ | ||
public class AvroSerializerConcurrencyCheckInactiveITCase { | ||
|
||
// this sets the debug initialization back to its default, even if | ||
// by default tests modify it (implicitly via assertion loading) | ||
static { | ||
AvroSerializerDebugInitHelper.setToDebug = AvroSerializerDebugInitHelper.INITIAL_SETTING; | ||
} | ||
|
||
/** | ||
* This test checks that concurrent access is not detected by default, meaning that | ||
* the thread concurrency checks are off by default. | ||
*/ | ||
@Test | ||
public void testWithNoConcurrencyCheck() throws Exception { | ||
boolean assertionError; | ||
try { | ||
new AvroSerializerConcurrencyTest().testConcurrentUseOfSerializer(); | ||
assertionError = false; | ||
} | ||
catch (AssertionError e) { | ||
assertionError = true; | ||
} | ||
|
||
assertTrue("testConcurrentUseOfSerializer() should have failed if " + | ||
"concurrency checks are off by default", assertionError); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
.../src/test/java/org/apache/flink/formats/avro/typeutils/AvroSerializerConcurrencyTest.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,94 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* https://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 org.apache.flink.formats.avro.typeutils; | ||
|
||
import org.apache.flink.core.memory.DataOutputSerializer; | ||
import org.apache.flink.core.memory.DataOutputView; | ||
import org.apache.flink.core.testutils.BlockerSync; | ||
import org.apache.flink.core.testutils.CheckedThread; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* This tests that the {@link AvroSerializer} properly fails when accessed by two threads | ||
* concurrently. | ||
* | ||
* <p><b>Important:</b> This test only works if assertions are activated (-ea) on the JVM | ||
* when running tests. | ||
*/ | ||
public class AvroSerializerConcurrencyTest { | ||
|
||
@Test | ||
public void testConcurrentUseOfSerializer() throws Exception { | ||
final AvroSerializer<String> serializer = new AvroSerializer<>(String.class); | ||
|
||
final BlockerSync sync = new BlockerSync(); | ||
|
||
final DataOutputView regularOut = new DataOutputSerializer(32); | ||
final DataOutputView lockingOut = new LockingView(sync); | ||
|
||
// this thread serializes and gets stuck there | ||
final CheckedThread thread = new CheckedThread("serializer") { | ||
@Override | ||
public void go() throws Exception { | ||
serializer.serialize("a value", lockingOut); | ||
} | ||
}; | ||
|
||
thread.start(); | ||
sync.awaitBlocker(); | ||
|
||
// this should fail with an exception | ||
try { | ||
serializer.serialize("value", regularOut); | ||
fail("should have failed with an exception"); | ||
} | ||
catch (IllegalStateException e) { | ||
// expected | ||
} | ||
finally { | ||
// release the thread that serializes | ||
sync.releaseBlocker(); | ||
} | ||
|
||
// this propagates exceptions from the spawned thread | ||
thread.sync(); | ||
} | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
private static class LockingView extends DataOutputSerializer { | ||
|
||
private final BlockerSync blocker; | ||
|
||
LockingView(BlockerSync blocker) { | ||
super(32); | ||
this.blocker = blocker; | ||
} | ||
|
||
@Override | ||
public void writeInt(int v) throws IOException { | ||
blocker.blockNonInterruptible(); | ||
} | ||
} | ||
} |
Oops, something went wrong.