Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shorter timeout on test_connection #18001

Merged
merged 5 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ flask-wtf==0.14.3
# via
# apache-superset
# flask-appbuilder
func-timeout==4.3.5
# via apache-superset
geographiclib==1.52
# via geopy
geopy==2.2.0
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def get_git_sha() -> str:
"flask-talisman",
"flask-migrate",
"flask-wtf",
"func_timeout",
"geopy",
"graphlib-backport",
"gunicorn>=20.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ const SqlAlchemyTab = ({
testConnection,
conf,
isEditMode = false,
testInProgress = false,
}: {
db: DatabaseObject | null;
onInputChange: EventHandler<ChangeEvent<HTMLInputElement>>;
testConnection: EventHandler<MouseEvent<HTMLElement>>;
conf: { SQLALCHEMY_DOCS_URL: string; SQLALCHEMY_DISPLAY_TEXT: string };
isEditMode?: boolean;
testInProgress?: boolean;
}) => (
<>
<StyledInputContainer>
Expand Down Expand Up @@ -88,6 +90,7 @@ const SqlAlchemyTab = ({
</StyledInputContainer>
<Button
onClick={testConnection}
disabled={testInProgress}
cta
buttonStyle="link"
css={(theme: SupersetTheme) => wideButton(theme)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
const [dbName, setDbName] = useState('');
const [editNewDb, setEditNewDb] = useState<boolean>(false);
const [isLoading, setLoading] = useState<boolean>(false);
const [testInProgress, setTestInProgress] = useState<boolean>(false);
const conf = useCommonConf();
const dbImages = getDatabaseImages();
const connectionAlert = getConnectionAlert();
Expand Down Expand Up @@ -494,7 +495,18 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
encrypted_extra: db?.encrypted_extra || '',
server_cert: db?.server_cert || undefined,
};
testDatabaseConnection(connection, addDangerToast, addSuccessToast);
setTestInProgress(true);
testDatabaseConnection(
connection,
(errorMsg: string) => {
setTestInProgress(false);
addDangerToast(errorMsg);
},
(errorMsg: string) => {
setTestInProgress(false);
addSuccessToast(errorMsg);
},
);
};

const onClose = () => {
Expand Down Expand Up @@ -1047,6 +1059,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
conf={conf}
testConnection={testConnection}
isEditMode={isEditMode}
testInProgress={testInProgress}
/>
{isDynamic(db?.backend || db?.engine) && !isEditMode && (
<div css={(theme: SupersetTheme) => infoTooltip(theme)}>
Expand Down
5 changes: 5 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,11 @@ def SQL_QUERY_MUTATOR( # pylint: disable=invalid-name,unused-argument
"SQLite",
# etc.
]
# When adding a new database we try to connect to it. Depending on which parameters are
# incorrect this could take a couple minutes, until the SQLAlchemy driver pinging the
# database times out. Instead of relying on the driver timeout we can specify a shorter
# one here.
TEST_DATABASE_CONNECTION_TIMEOUT = timedelta(seconds=30)

# Do you want Talisman enabled?
TALISMAN_ENABLED = False
Expand Down
20 changes: 20 additions & 0 deletions superset/databases/commands/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
# specific language governing permissions and limitations
# under the License.
import logging
import sqlite3
from contextlib import closing
from typing import Any, Dict, Optional

from flask import current_app as app
from flask_appbuilder.security.sqla.models import User
from flask_babel import gettext as _
from func_timeout import func_timeout, FunctionTimedOut
from sqlalchemy.engine.url import make_url
from sqlalchemy.exc import DBAPIError, NoSuchModuleError

Expand Down Expand Up @@ -78,7 +81,24 @@ def run(self) -> None:
)
with closing(engine.raw_connection()) as conn:
try:
alive = func_timeout(
betodealmeida marked this conversation as resolved.
Show resolved Hide resolved
int(
app.config[
"TEST_DATABASE_CONNECTION_TIMEOUT"
].total_seconds()
),
engine.dialect.do_ping,
args=(conn,),
)
except sqlite3.ProgrammingError:
# SQLite can't run on a separate thread, so ``func_timeout`` fails
alive = engine.dialect.do_ping(conn)
except FunctionTimedOut as ex:
raise Exception(
"Please check your connection details and database settings, "
"and ensure that your database is accepting connections, then "
"try connecting again."
) from ex
except Exception: # pylint: disable=broad-except
alive = False
if not alive:
Expand Down