From 1fb37e8ae897595e136b2dc1e27232a3dacb7297 Mon Sep 17 00:00:00 2001 From: yuezhang Date: Tue, 19 Jan 2021 16:07:47 +0800 Subject: [PATCH 1/2] enhance the logic of Start up DruidSchema immediately if there are no segments. --- .../java/org/apache/druid/sql/calcite/schema/DruidSchema.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/schema/DruidSchema.java b/sql/src/main/java/org/apache/druid/sql/calcite/schema/DruidSchema.java index 215120aa02bd..1e4321b4611d 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/schema/DruidSchema.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/schema/DruidSchema.java @@ -242,7 +242,9 @@ public void start() throws InterruptedException break; } - if (isServerViewInitialized) { + // lastFailure != 0L means exceptions happened before and there're some refresh work was not completed. + // so that even ServerView is initialized, we can't let broker complete initialization. + if (isServerViewInitialized && lastFailure == 0L) { // Server view is initialized, but we don't need to do a refresh. Could happen if there are // no segments in the system yet. Just mark us as initialized, then. initialized.countDown(); From fb1a7fcd03895e20dfab07f0654f9ae77f28d811 Mon Sep 17 00:00:00 2001 From: yuezhang Date: Wed, 20 Jan 2021 18:21:37 +0800 Subject: [PATCH 2/2] add UT to test DruidSchema init --- .../sql/calcite/schema/DruidSchemaTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/schema/DruidSchemaTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/schema/DruidSchemaTest.java index 9b633e8a19e1..0863cd97fb13 100644 --- a/sql/src/test/java/org/apache/druid/sql/calcite/schema/DruidSchemaTest.java +++ b/sql/src/test/java/org/apache/druid/sql/calcite/schema/DruidSchemaTest.java @@ -137,6 +137,7 @@ public static void tearDownClass() throws IOException private SpecificSegmentsQuerySegmentWalker walker = null; private DruidSchema schema = null; + private DruidSchema schema2 = null; private SegmentManager segmentManager; private Set segmentDataSourceNames; private Set joinableDataSourceNames; @@ -269,6 +270,38 @@ protected DruidTable buildDruidTable(String dataSource) } }; + schema2 = new DruidSchema( + CalciteTests.createMockQueryLifecycleFactory(walker, conglomerate), + serverView, + segmentManager, + new MapJoinableFactory(ImmutableSet.of(globalTableJoinable), ImmutableMap.of(globalTableJoinable.getClass(), GlobalTableDataSource.class)), + PLANNER_CONFIG_DEFAULT, + new NoopViewManager(), + new NoopEscalator() + ) + { + + boolean throwException = true; + @Override + protected DruidTable buildDruidTable(String dataSource) + { + DruidTable table = super.buildDruidTable(dataSource); + buildTableLatch.countDown(); + return table; + } + + @Override + Set refreshSegments(final Set segments) throws IOException + { + if (throwException) { + throwException = false; + throw new RuntimeException("Query[xxxx] url[http://xxxx:8083/druid/v2/] timed out."); + } else { + return super.refreshSegments(segments); + } + } + }; + schema.start(); schema.awaitInitialization(); } @@ -289,6 +322,19 @@ public void testGetTableMap() Assert.assertEquals(ImmutableSet.of("foo", "foo2"), tableMap.keySet()); } + @Test + public void testSchemaInit() throws InterruptedException + { + schema2.start(); + schema2.awaitInitialization(); + Map tableMap = schema2.getTableMap(); + Assert.assertEquals(2, tableMap.size()); + Assert.assertTrue(tableMap.containsKey("foo")); + Assert.assertTrue(tableMap.containsKey("foo2")); + schema2.stop(); + } + + @Test public void testGetTableMapFoo() {