You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2020/04/30 22:20:24 UTC

[GitHub] [couchdb] jiangphcn opened a new pull request #2857: Background database deletion

jiangphcn opened a new pull request #2857:
URL: https://github.com/apache/couchdb/pull/2857


   <!-- Thank you for your contribution!
   
        Please file this form by replacing the Markdown comments
        with your text. If a section needs no action - remove it.
   
        Also remember, that CouchDB uses the Review-Then-Commit (RTC) model
        of code collaboration. Positive feedback is represented +1 from committers
        and negative is a -1. The -1 also means veto, and needs to be addressed
        to proceed. Once there are no objections, the PR can be merged by a
        CouchDB committer.
   
        See: http://couchdb.apache.org/bylaws.html#decisions for more info. -->
   
   ## Overview
   
   <!-- Please give a short brief for the pull request,
        what problem it solves or how it makes things better. -->
   
   Allow to permanently delete those soft-deleted databases under background. In general, the soft-deleted database can be kept from several hours to several days. Later, they need to be deleted to reclaim disk space. So one criteria can be specified to decide how to select which soft-deleted databases to be deleted.
   
   ## Testing recommendations
   
   <!-- Describe how we can test your changes.
        Does it provides any behaviour that the end users
        could notice? -->
   
   ## Related Issues or Pull Requests
   
   <!-- If your changes affects multiple components in different
        repositories please put links to those issues or pull requests here.  -->
   tbd
   
   ## Checklist
   
   - [ ] Code is written and works correctly
   - [ ] Changes are covered by tests
   - [ ] Any new configurable parameters are documented in `rel/overlay/etc/default.ini`
   - [ ] A PR for documentation changes has been made in https://github.com/apache/couchdb-documentation
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] nickva commented on a change in pull request #2857: Background database deletion

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2857:
URL: https://github.com/apache/couchdb/pull/2857#discussion_r418322037



##########
File path: src/fabric/src/fabric2_db_expiration.erl
##########
@@ -0,0 +1,218 @@
+% Licensed 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.
+
+-module(fabric2_db_expiration).
+
+
+-behaviour(gen_server).
+
+
+-export([
+    start_link/0
+]).
+
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2,
+    code_change/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("fabric/include/fabric2.hrl").
+
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+
+init(_) ->
+    start_timer(),
+    {ok, nil}.
+
+
+terminate(_M, _St) ->
+    ok.
+
+
+handle_call(Msg, _From, St) ->
+    {stop, {bad_call, Msg}, {bad_call, Msg}, St}.
+
+
+handle_cast(timeout, St) ->
+    proc_lib:init_ack({ok, self()}),

Review comment:
       Once we have a gen_server let's just stick with it and not worry about proc_lib. So let's skip it altogether




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] nickva commented on a change in pull request #2857: Background database deletion

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2857:
URL: https://github.com/apache/couchdb/pull/2857#discussion_r418324493



##########
File path: src/fabric/src/fabric2_db_expiration.erl
##########
@@ -0,0 +1,218 @@
+% Licensed 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.
+
+-module(fabric2_db_expiration).
+
+
+-behaviour(gen_server).
+
+
+-export([
+    start_link/0
+]).
+
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2,
+    code_change/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("fabric/include/fabric2.hrl").
+
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+
+init(_) ->
+    start_timer(),
+    {ok, nil}.

Review comment:
       For the initial `timeout` we can use the trick where the `gen_server`'s `init()` returns `{ok, State, TimeoutMSec}`
   
   If we put say 0 for the timeout then after init returns a `timeout` message will be sent to handle_info. So we don't even have to the start_timer trick at all.
   
   In the handle_info(timeout, ...) we could call `wait_couch_jobs()` and `add_or_get_job()`
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] nickva commented on a change in pull request #2857: Background database deletion

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2857:
URL: https://github.com/apache/couchdb/pull/2857#discussion_r418325821



##########
File path: src/fabric/src/fabric2_db_expiration.erl
##########
@@ -0,0 +1,218 @@
+% Licensed 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.
+
+-module(fabric2_db_expiration).
+
+
+-behaviour(gen_server).
+
+
+-export([
+    start_link/0
+]).
+
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2,
+    code_change/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("fabric/include/fabric2.hrl").
+
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+
+init(_) ->
+    start_timer(),
+    {ok, nil}.
+
+
+terminate(_M, _St) ->
+    ok.
+
+
+handle_call(Msg, _From, St) ->
+    {stop, {bad_call, Msg}, {bad_call, Msg}, St}.
+
+
+handle_cast(timeout, St) ->
+    proc_lib:init_ack({ok, self()}),
+    try
+        couch_jobs:set_type_timeout(?DB_EXPIRATION_JOB_TYPE, 6),
+        case add_or_get_job() of
+            ok -> ok;
+            retry -> add_or_get_job()
+        end,
+        case run_loop() of
+            ok -> ok;
+            retry -> run_loop()
+        end,
+        {noreply, St}
+    catch
+        _:_->
+            start_timer(),
+            {noreply, St}
+    end;
+handle_cast(Msg, St) ->
+    {stop, {bad_cast, Msg}, St}.
+
+
+handle_info(Msg, St) ->
+    {stop, {bad_info, Msg}, St}.
+
+
+code_change(_OldVsn, St, _Extra) ->
+    {ok, St}.
+
+
+start_timer() ->
+    After = 10,
+    case timer:apply_after(After, gen_server, cast, [self(), timeout]) of
+        {ok, Ref} ->
+            Ref;
+        _Error ->
+            nil
+    end.
+
+
+add_or_get_job() ->
+    couch_jobs:set_type_timeout(?DB_EXPIRATION_JOB_TYPE, 6),
+    case couch_jobs:get_job_data(
+        undefined,
+        ?DB_EXPIRATION_JOB_TYPE,
+        ?DB_EXPIRATION_JOB
+    ) of
+        {error, not_found} ->
+            couch_jobs:add(
+                undefined,
+                ?DB_EXPIRATION_JOB_TYPE,
+                ?DB_EXPIRATION_JOB,
+                #{}
+            );
+        {ok, _JobData} ->
+            ok
+    end.
+
+
+run_loop() ->
+    {ok, Job, JobData} = couch_jobs:accept(?DB_EXPIRATION_JOB_TYPE),

Review comment:
       Since we sticking with a gen_server (instead of the proc_lib hack), we don't want to block during accept so we can 
    say `spawn_monitor` a new process which will run ` {ok, Job, JobData} = couch_jobs:accept(?DB_EXPIRATION_JOB_TYPE, #{max_scheduled_time => now_sec()})` and exit with the `{Job, JobData}` result or error. To be extra sure we keep track of it we can keep the `Ref` in the gen_server state (instead of making it nil it could be `#st{acceptor = Ref}` for example.
   
   Then in the main gen_server we'd wait for the 'DOWN' message from the acceptor. When we get it we'd call `process_expiration()`, `couch_jobs:resubmit(undefined, Job, Now + schedule_sec(), JobData),` and spawn another acceptor again and so on.
   
   This way we are more verbose however it's more obvious what is happening
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] nickva commented on a change in pull request #2857: Background database deletion

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2857:
URL: https://github.com/apache/couchdb/pull/2857#discussion_r418326689



##########
File path: src/fabric/src/fabric2_db_expiration.erl
##########
@@ -0,0 +1,218 @@
+% Licensed 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.
+
+-module(fabric2_db_expiration).
+
+
+-behaviour(gen_server).
+
+
+-export([
+    start_link/0
+]).
+
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2,
+    code_change/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("fabric/include/fabric2.hrl").
+
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+
+init(_) ->
+    start_timer(),
+    {ok, nil}.
+
+
+terminate(_M, _St) ->
+    ok.
+
+
+handle_call(Msg, _From, St) ->
+    {stop, {bad_call, Msg}, {bad_call, Msg}, St}.
+
+
+handle_cast(timeout, St) ->
+    proc_lib:init_ack({ok, self()}),
+    try
+        couch_jobs:set_type_timeout(?DB_EXPIRATION_JOB_TYPE, 6),
+        case add_or_get_job() of
+            ok -> ok;
+            retry -> add_or_get_job()
+        end,
+        case run_loop() of
+            ok -> ok;
+            retry -> run_loop()
+        end,
+        {noreply, St}
+    catch
+        _:_->
+            start_timer(),
+            {noreply, St}
+    end;
+handle_cast(Msg, St) ->
+    {stop, {bad_cast, Msg}, St}.
+
+
+handle_info(Msg, St) ->
+    {stop, {bad_info, Msg}, St}.
+
+
+code_change(_OldVsn, St, _Extra) ->
+    {ok, St}.
+
+
+start_timer() ->
+    After = 10,
+    case timer:apply_after(After, gen_server, cast, [self(), timeout]) of
+        {ok, Ref} ->
+            Ref;
+        _Error ->
+            nil
+    end.
+
+
+add_or_get_job() ->
+    couch_jobs:set_type_timeout(?DB_EXPIRATION_JOB_TYPE, 6),

Review comment:
       For checking couch_jobs application is running we could use this trick as well:
   
   ```
    lists:keysearch(couch_jobs, 1, application:which_applications()).
   ```
   
   The contract the `which_applications/0` says that it return running application. One thing to check first would be if it start returning the application while it is still initializing or only after it finished and is all ready to go. The last option would be idea. So then before add_or_get_job() we'd add something like "wait_for_couch_jobs_app()" loop  perhaps.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] nickva commented on a change in pull request #2857: Background database deletion

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2857:
URL: https://github.com/apache/couchdb/pull/2857#discussion_r418326689



##########
File path: src/fabric/src/fabric2_db_expiration.erl
##########
@@ -0,0 +1,218 @@
+% Licensed 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.
+
+-module(fabric2_db_expiration).
+
+
+-behaviour(gen_server).
+
+
+-export([
+    start_link/0
+]).
+
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2,
+    code_change/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("fabric/include/fabric2.hrl").
+
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+
+init(_) ->
+    start_timer(),
+    {ok, nil}.
+
+
+terminate(_M, _St) ->
+    ok.
+
+
+handle_call(Msg, _From, St) ->
+    {stop, {bad_call, Msg}, {bad_call, Msg}, St}.
+
+
+handle_cast(timeout, St) ->
+    proc_lib:init_ack({ok, self()}),
+    try
+        couch_jobs:set_type_timeout(?DB_EXPIRATION_JOB_TYPE, 6),
+        case add_or_get_job() of
+            ok -> ok;
+            retry -> add_or_get_job()
+        end,
+        case run_loop() of
+            ok -> ok;
+            retry -> run_loop()
+        end,
+        {noreply, St}
+    catch
+        _:_->
+            start_timer(),
+            {noreply, St}
+    end;
+handle_cast(Msg, St) ->
+    {stop, {bad_cast, Msg}, St}.
+
+
+handle_info(Msg, St) ->
+    {stop, {bad_info, Msg}, St}.
+
+
+code_change(_OldVsn, St, _Extra) ->
+    {ok, St}.
+
+
+start_timer() ->
+    After = 10,
+    case timer:apply_after(After, gen_server, cast, [self(), timeout]) of
+        {ok, Ref} ->
+            Ref;
+        _Error ->
+            nil
+    end.
+
+
+add_or_get_job() ->
+    couch_jobs:set_type_timeout(?DB_EXPIRATION_JOB_TYPE, 6),

Review comment:
       For checking couch_jobs application is running we could use this trick as well:
   
   ```
    lists:keysearch(couch_jobs, 1, application:which_applications()).
   ```
   
   The contract in the `which_applications/0` says that it returns running applications. One thing to check first would be if we start returning the application while it is still initializing or only after it finished and is all ready to go. The last option would be idea. So then before add_or_get_job() we'd add something like "wait_for_couch_jobs_app()" loop  perhaps.
   
   I checked this by adding a sleep in one of the init function in fabric2 and then ran `application:which_applications()` and it didn't appear there while it was still initializing. So we should be good using this




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org