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 40dcb56ee84..0e7e9734646 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 @@ -207,8 +207,17 @@ public class DruidSchema extends AbstractSchema !wasRecentFailure && (!segmentsNeedingRefresh.isEmpty() || !dataSourcesNeedingRebuild.isEmpty()) && (refreshImmediately || nextRefresh < System.currentTimeMillis())) { + // We need to do a refresh. Break out of the waiting loop. break; } + + if (isServerViewInitialized) { + // 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(); + } + + // Wait some more, we'll wake up when it might be time to do another refresh. lock.wait(Math.max(1, nextRefresh - System.currentTimeMillis())); } diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/schema/DruidSchemaNoDataInitTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/schema/DruidSchemaNoDataInitTest.java new file mode 100644 index 00000000000..74ac383ad4f --- /dev/null +++ b/sql/src/test/java/org/apache/druid/sql/calcite/schema/DruidSchemaNoDataInitTest.java @@ -0,0 +1,69 @@ +/* + * 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 + * + * 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 org.apache.druid.sql.calcite.schema; + +import com.google.common.collect.ImmutableMap; +import org.apache.druid.java.util.common.Pair; +import org.apache.druid.java.util.common.io.Closer; +import org.apache.druid.query.QueryRunnerFactoryConglomerate; +import org.apache.druid.server.security.NoopEscalator; +import org.apache.druid.sql.calcite.planner.PlannerConfig; +import org.apache.druid.sql.calcite.util.CalciteTestBase; +import org.apache.druid.sql.calcite.util.CalciteTests; +import org.apache.druid.sql.calcite.util.SpecificSegmentsQuerySegmentWalker; +import org.apache.druid.sql.calcite.util.TestServerInventoryView; +import org.apache.druid.sql.calcite.view.NoopViewManager; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; + +public class DruidSchemaNoDataInitTest extends CalciteTestBase +{ + private static final PlannerConfig PLANNER_CONFIG_DEFAULT = new PlannerConfig(); + + @Test + public void testInitializationWithNoData() throws Exception + { + final Pair conglomerateCloserPair = CalciteTests + .createQueryRunnerFactoryConglomerate(); + + try { + final DruidSchema druidSchema = new DruidSchema( + CalciteTests.createMockQueryLifecycleFactory( + new SpecificSegmentsQuerySegmentWalker(conglomerateCloserPair.lhs), + conglomerateCloserPair.lhs + ), + new TestServerInventoryView(Collections.emptyList()), + PLANNER_CONFIG_DEFAULT, + new NoopViewManager(), + new NoopEscalator() + ); + + druidSchema.start(); + druidSchema.awaitInitialization(); + + Assert.assertEquals(ImmutableMap.of(), druidSchema.getTableMap()); + } + finally { + conglomerateCloserPair.rhs.close(); + } + } +}