Sandor Molnar created KNOX-3593:
-----------------------------------

             Summary: KnoxCLI: create the TokenStateService only for the token 
migration commands
                 Key: KNOX-3593
                 URL: https://issues.apache.org/jira/browse/KNOX-3593
             Project: Apache Knox
          Issue Type: Improvement
    Affects Versions: 3.0.0, 2.1.0, 2.0.0
            Reporter: Sandor Molnar
            Assignee: Sandor Molnar
             Fix For: 3.1.0


h3. Problem

{{CLIGatewayServices#init}} creates the {{TokenStateService}} for every KnoxCLI 
command. It was added in KNOX-2990 for {{migrate-tokens}}, and 
{{migrate-derby-tokens}} uses it as well. No other command needs it.

With the default H2 or a JDBC backend, creating the service opens the token 
database. So every command, e.g. {{create-alias}} or {{list-alias}}, does the 
following:
* It connects to the database and creates the token tables if they are missing.
* With embedded H2, it writes the {{gateway_database_user}} and 
{{gateway_database_password}} aliases to the gateway credential store and 
creates the database folder.
* If the gateway is running, it can't open the embedded H2 file because the 
gateway holds the lock. It logs an error and falls back to 
{{DefaultTokenStateService}}. Before KNOX-3592 this failed at once.
* With KNOX-3592's connection pool, the same failure blocked the command for 
the full connection timeout (30 s). That broke the CI E2E tests, which run 
{{knoxcli create-alias}} while the gateway is up. KNOX-3592 now makes the pool 
fail fast, which brings back the immediate failure, but the database access 
itself is still not needed.
* With an external database (e.g. Postgres), every command opens a database 
connection, and the pool may open up to {{gateway.database.pool.max.size}} 
connections until the JVM exits.

h3. Proposal

Create the {{TokenStateService}} in KnoxCLI only when a command asks for it, 
i.e. {{migrate-tokens}} and {{migrate-derby-tokens}}. Options:
* {{CLIGatewayServices}} keeps the config and options from {{init}} and creates 
the service on the first {{getService(ServiceType.TOKEN_STATE_SERVICE)}} call.
* Or the two migration commands create the service themselves through 
{{GatewayServiceFactory}}.

Nothing else changes: all other commands run without opening the token database.

h3. Testing

* {{KnoxCLITest}}: {{create-alias}}, {{list-alias}} and the other commands 
don't create a {{TokenStateService}} or open the database. With embedded H2, no 
{{gateway_database_*}} aliases are written.
* {{migrate-tokens}} and {{migrate-derby-tokens}} still get the configured 
JDBC/H2 {{TokenStateService}}, and their existing tests pass.
* Manual: with the gateway running on embedded H2, {{knoxcli create-alias}} 
completes without H2 lock errors in {{knoxcli.log}}.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to