Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.avatica;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.inject.Inject;
import com.google.inject.Injector;
import org.apache.calcite.avatica.MetaImpl;
import org.apache.calcite.avatica.MissingResultsException;
import org.apache.calcite.avatica.NoSuchConnectionException;
import org.apache.calcite.avatica.NoSuchStatementException;
import org.apache.calcite.avatica.QueryState;
import org.apache.calcite.avatica.remote.TypedValue;
import org.apache.druid.java.util.common.DateTimes;
import org.apache.druid.java.util.common.ISE;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.logger.Logger;
import org.apache.druid.server.security.AuthenticationResult;
import org.apache.druid.server.security.Authenticator;
import org.apache.druid.server.security.AuthenticatorMapper;
import org.apache.druid.server.security.ForbiddenException;
import org.apache.druid.sql.SqlLifecycleFactory;
import org.apache.druid.sql.calcite.planner.Calcites;
import org.joda.time.Interval;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class DruidMeta extends MetaImpl
{
private static final Logger log = new Logger(DruidMeta.class);
private final SqlLifecycleFactory sqlLifecycleFactory;
private final ScheduledExecutorService exec;
private final AvaticaServerConfig config;
private final List authenticators;
/** Used to track logical connections. */
private final ConcurrentMap connections = new ConcurrentHashMap<>();
/**
* Number of connections reserved in "connections". May be higher than the actual number of connections at times,
* such as when we're reserving space to open a new one.
*/
private final AtomicInteger connectionCount = new AtomicInteger();
@Inject
public DruidMeta(
final SqlLifecycleFactory sqlLifecycleFactory,
final AvaticaServerConfig config,
final Injector injector
)
{
super(null);
this.sqlLifecycleFactory = Preconditions.checkNotNull(sqlLifecycleFactory, "sqlLifecycleFactory");
this.config = config;
this.exec = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setNameFormat(StringUtils.format("DruidMeta@%s-ScheduledExecutor", Integer.toHexString(hashCode())))
.setDaemon(true)
.build()
);
final AuthenticatorMapper authenticatorMapper = injector.getInstance(AuthenticatorMapper.class);
this.authenticators = authenticatorMapper.getAuthenticatorChain();
}
@Override
public void openConnection(final ConnectionHandle ch, final Map info)
{
// Build connection context.
final ImmutableMap.Builder context = ImmutableMap.builder();
for (Map.Entry entry : info.entrySet()) {
context.put(entry);
}
openDruidConnection(ch.id, context.build());
}
@Override
public void closeConnection(final ConnectionHandle ch)
{
final DruidConnection druidConnection = connections.remove(ch.id);
if (druidConnection != null) {
connectionCount.decrementAndGet();
druidConnection.close();
}
}
@Override
public ConnectionProperties connectionSync(final ConnectionHandle ch, final ConnectionProperties connProps)
{
// getDruidConnection re-syncs it.
getDruidConnection(ch.id);
return connProps;
}
@Override
public StatementHandle createStatement(final ConnectionHandle ch)
{
final DruidStatement druidStatement = getDruidConnection(ch.id).createStatement(sqlLifecycleFactory);
return new StatementHandle(ch.id, druidStatement.getStatementId(), null);
}
@Override
public StatementHandle prepare(
final ConnectionHandle ch,
final String sql,
final long maxRowCount
)
{
final StatementHandle statement = createStatement(ch);
final DruidStatement druidStatement;
try {
druidStatement = getDruidStatement(statement);
}
catch (NoSuchStatementException e) {
throw new IllegalStateException(e);
}
final DruidConnection druidConnection = getDruidConnection(statement.connectionId);
AuthenticationResult authenticationResult = authenticateConnection(druidConnection);
if (authenticationResult == null) {
throw new ForbiddenException("Authentication failed.");
}
statement.signature = druidStatement.prepare(sql, maxRowCount, authenticationResult).getSignature();
return statement;
}
@Deprecated
@Override
public ExecuteResult prepareAndExecute(
final StatementHandle h,
final String sql,
final long maxRowCount,
final PrepareCallback callback
)
{
// Avatica doesn't call this.
throw new UnsupportedOperationException("Deprecated");
}
@Override
public ExecuteResult prepareAndExecute(
final StatementHandle statement,
final String sql,
final long maxRowCount,
final int maxRowsInFirstFrame,
final PrepareCallback callback
) throws NoSuchStatementException
{
// Ignore "callback", this class is designed for use with LocalService which doesn't use it.
final DruidStatement druidStatement = getDruidStatement(statement);
final DruidConnection druidConnection = getDruidConnection(statement.connectionId);
AuthenticationResult authenticationResult = authenticateConnection(druidConnection);
if (authenticationResult == null) {
throw new ForbiddenException("Authentication failed.");
}
final Signature signature = druidStatement.prepare(sql, maxRowCount, authenticationResult).getSignature();
final Frame firstFrame = druidStatement.execute()
.nextFrame(
DruidStatement.START_OFFSET,
getEffectiveMaxRowsPerFrame(maxRowsInFirstFrame)
);
return new ExecuteResult(
ImmutableList.of(
MetaResultSet.create(
statement.connectionId,
statement.id,
false,
signature,
firstFrame
)
)
);
}
@Override
public ExecuteBatchResult prepareAndExecuteBatch(
final StatementHandle statement,
final List sqlCommands
)
{
// Batch statements are used for bulk updates, but we don't support updates.
throw new UnsupportedOperationException("Batch statements not supported");
}
@Override
public ExecuteBatchResult executeBatch(
final StatementHandle statement,
final List> parameterValues
)
{
// Batch statements are used for bulk updates, but we don't support updates.
throw new UnsupportedOperationException("Batch statements not supported");
}
@Override
public Frame fetch(
final StatementHandle statement,
final long offset,
final int fetchMaxRowCount
) throws NoSuchStatementException, MissingResultsException
{
return getDruidStatement(statement).nextFrame(offset, getEffectiveMaxRowsPerFrame(fetchMaxRowCount));
}
@Deprecated
@Override
public ExecuteResult execute(
final StatementHandle statement,
final List parameterValues,
final long maxRowCount
)
{
// Avatica doesn't call this.
throw new UnsupportedOperationException("Deprecated");
}
@Override
public ExecuteResult execute(
final StatementHandle statement,
final List parameterValues,
final int maxRowsInFirstFrame
) throws NoSuchStatementException
{
Preconditions.checkArgument(parameterValues.isEmpty(), "Expected parameterValues to be empty");
final DruidStatement druidStatement = getDruidStatement(statement);
final Signature signature = druidStatement.getSignature();
final Frame firstFrame = druidStatement.execute()
.nextFrame(
DruidStatement.START_OFFSET,
getEffectiveMaxRowsPerFrame(maxRowsInFirstFrame)
);
return new ExecuteResult(
ImmutableList.of(
MetaResultSet.create(
statement.connectionId,
statement.id,
false,
signature,
firstFrame
)
)
);
}
@Override
public Iterable