You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2023/06/29 14:57:35 UTC

[couchdb] branch out-of-disk-handler updated (b4acfddd2 -> 48abc5a12)

This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a change to branch out-of-disk-handler
in repository https://gitbox.apache.org/repos/asf/couchdb.git


 discard b4acfddd2 WIP Introduce countermeasures as we run out of disk space
     new 48abc5a12 WIP Introduce countermeasures as we run out of disk space

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (b4acfddd2)
            \
             N -- N -- N   refs/heads/out-of-disk-handler (48abc5a12)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/couch/src/couch_alarm_handler.erl | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)


[couchdb] 01/01: WIP Introduce countermeasures as we run out of disk space

Posted by rn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch out-of-disk-handler
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 48abc5a1211f9a1f0e522af318ed15fef34aa410
Author: Robert Newson <rn...@apache.org>
AuthorDate: Tue Jun 20 11:52:46 2023 +0100

    WIP Introduce countermeasures as we run out of disk space
---
 rel/reltool.config                         |   1 +
 src/chttpd/src/chttpd.erl                  |   2 +
 src/couch/priv/stats_descriptions.cfg      |   4 +
 src/couch/src/couch_alarm_handler.erl      | 167 +++++++++++++++++++++++++++++
 src/couch/src/couch_app.erl                |   3 +
 src/couch_mrview/src/couch_mrview.erl      |   8 +-
 src/couch_mrview/src/couch_mrview_util.erl |  17 ++-
 src/fabric/src/fabric_doc_update.erl       |   2 +
 src/fabric/src/fabric_rpc.erl              |  44 +++++---
 src/fabric/src/fabric_streams.erl          |   4 +-
 src/fabric/src/fabric_view.erl             |   8 +-
 src/fabric/src/fabric_view_map.erl         |   4 +-
 src/fabric/src/fabric_view_reduce.erl      |   4 +
 src/ken/src/ken_server.erl                 |   3 +-
 14 files changed, 243 insertions(+), 28 deletions(-)

diff --git a/rel/reltool.config b/rel/reltool.config
index 0355a0b07..ebb15bb83 100644
--- a/rel/reltool.config
+++ b/rel/reltool.config
@@ -87,6 +87,7 @@
     {app, sasl, [{incl_cond, include}]},
     {app, ssl, [{incl_cond, include}]},
     {app, stdlib, [{incl_cond, include}]},
+    {app, os_mon, [{incl_cond, include}]},
     {app, syntax_tools, [{incl_cond, include}]},
     {app, xmerl, [{incl_cond, include}]},
 
diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index 53abc731f..c8e6fdc97 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -1138,6 +1138,8 @@ error_info(timeout) ->
     >>};
 error_info({service_unavailable, Reason}) ->
     {503, <<"service unavailable">>, Reason};
+error_info({insufficient_storage, Reason}) ->
+    {507, <<"insufficent_storage">>, Reason};
 error_info({timeout, _Reason}) ->
     error_info(timeout);
 error_info({'EXIT', {Error, _Stack}}) ->
diff --git a/src/couch/priv/stats_descriptions.cfg b/src/couch/priv/stats_descriptions.cfg
index 6c0d4dad2..1983eed9b 100644
--- a/src/couch/priv/stats_descriptions.cfg
+++ b/src/couch/priv/stats_descriptions.cfg
@@ -266,6 +266,10 @@
     {type, counter},
     {desc, <<"number of HTTP 503 Service unavailable responses">>}
 ]}.
+{[couchdb, httpd_status_codes, 507], [
+    {type, counter},
+    {desc, <<"number of HTTP 507 Insufficient Storage responses">>}
+]}.
 {[couchdb, open_databases], [
     {type, counter},
     {desc,  <<"number of open databases">>}
diff --git a/src/couch/src/couch_alarm_handler.erl b/src/couch/src/couch_alarm_handler.erl
new file mode 100644
index 000000000..b239ecc3f
--- /dev/null
+++ b/src/couch/src/couch_alarm_handler.erl
@@ -0,0 +1,167 @@
+% 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(couch_alarm_handler).
+-behaviour(gen_event).
+
+% public api
+-export([
+    database_dir_almost_full/0,
+    view_index_dir_almost_full/0
+]).
+
+% gen_event callbacks
+-export([
+    init/1,
+    handle_event/2,
+    handle_call/2,
+    handle_info/2
+]).
+
+-include_lib("kernel/include/file.hrl").
+
+-record(st, {
+    timer
+}).
+
+database_dir_almost_full() ->
+    {DatabaseDirAlmostFull, _ViewIndexDirAlmostFull} = get_almost_full(),
+    DatabaseDirAlmostFull.
+
+view_index_dir_almost_full() ->
+    {_DatabaseDirAlmostFull, ViewIndexDirAlmostFull} = get_almost_full(),
+    ViewIndexDirAlmostFull.
+
+init(_Args) ->
+    {ok, #st{}}.
+
+handle_event({set_alarm, Alarm}, St) ->
+    {ok, set_alarm(Alarm, St)};
+handle_event({clear_alarm, AlarmId}, St) ->
+    {ok, clear_alarm(AlarmId, St)};
+handle_event(_Event, St) ->
+    {ok, St}.
+
+handle_call(_Query, St) ->
+    {ok, {error, bad_query}, St}.
+
+handle_info({disk_almost_full, DatabaseDirAlmostFull, ViewIndexDirAlmostFull}, St) ->
+    set_almost_full(DatabaseDirAlmostFull, ViewIndexDirAlmostFull),
+    {ok, St#st{timer = undefined}};
+handle_info(_Msg, St) ->
+    {ok, St}.
+
+set_alarm({{disk_almost_full, MntOn}, []}, #st{} = St) ->
+    IsDatabaseDir = is_database_dir(MntOn),
+    IsViewIndexDir = is_view_index_dir(MntOn),
+    {DatabaseDirAlmostFull, ViewIndexDirAlmostFull} = get_almost_full(),
+    if
+        IsDatabaseDir andalso IsViewIndexDir ->
+            couch_log:warning(
+                "database_dir and view_index_dir almost full", []
+            ),
+            start_timer(true, true, St);
+        IsDatabaseDir ->
+            couch_log:warning("database_dir almost full", []),
+            start_timer(true, ViewIndexDirAlmostFull, St);
+        IsViewIndexDir ->
+            couch_log:warning("view_index_dir almost full", []),
+            start_timer(DatabaseDirAlmostFull, true, St);
+        true ->
+            St
+    end;
+set_alarm(Alarm, #st{} = St) ->
+    error_logger:info_report([{alarm_handler, {set, Alarm}}]),
+    St.
+
+clear_alarm({disk_almost_full, MntOn}, #st{} = St) ->
+    IsDatabaseDir = is_database_dir(MntOn),
+    IsViewIndexDir = is_view_index_dir(MntOn),
+    {DatabaseDirAlmostFull, ViewIndexDirAlmostFull} = get_almost_full(),
+    if
+        IsDatabaseDir andalso IsViewIndexDir ->
+            couch_log:notice(
+                "database_dir and view_index_dir no longer almost full",
+                []
+            ),
+            start_timer(false, false, St);
+        IsDatabaseDir ->
+            couch_log:notice("database_dir no longer almost full", []),
+            start_timer(false, ViewIndexDirAlmostFull, St);
+        IsViewIndexDir ->
+            couch_log:notice(
+                "view_index_dir no longer almost full", []
+            ),
+            start_timer(DatabaseDirAlmostFull, false, St);
+        true ->
+            St
+    end;
+clear_alarm(AlarmId, #st{} = St) ->
+    error_logger:info_report([{alarm_handler, {clear, AlarmId}}]),
+    St.
+
+start_timer(DatabaseDirAlmostFull, ViewIndexDirAlmostFull, #st{timer = undefined} = St) ->
+    case timer:send_after(5000, {disk_almost_full, DatabaseDirAlmostFull, DatabaseDirAlmostFull}) of
+        {ok, TRef} ->
+            St#st{timer = TRef};
+        {error, Reason} ->
+            couch_log:warning("failed to delay for reason ~p", [Reason]),
+            set_almost_full(DatabaseDirAlmostFull, ViewIndexDirAlmostFull),
+            St
+    end;
+start_timer(DatabaseDirAlmostFull, ViewIndexDirAlmostFull, #st{timer = TRef} = St) ->
+    timer:cancel(TRef),
+    start_timer(DatabaseDirAlmostFull, ViewIndexDirAlmostFull, St#st{timer = undefined}).
+
+is_database_dir(MntOn) ->
+    same_device(config:get("couchdb", "database_dir"), MntOn).
+
+is_view_index_dir(MntOn) ->
+    same_device(config:get("couchdb", "view_index_dir"), MntOn).
+
+same_device(DirA, DirB) ->
+    case {device_id(DirA), device_id(DirB)} of
+        {{ok, DeviceId}, {ok, DeviceId}} ->
+            true;
+        _Else ->
+            false
+    end.
+
+device_id(Dir) ->
+    case file:read_file_info(Dir) of
+        {ok, FileInfo} ->
+            {ok, {FileInfo#file_info.minor_device, FileInfo#file_info.major_device}};
+        {error, Reason} ->
+            {error, Reason}
+    end.
+
+get_almost_full() ->
+    <<DatabaseDirAlmostFull:1, ViewIndexDirAlmostFull:1>> = persistent_term:get(key(), <<0:2>>),
+    {DatabaseDirAlmostFull == 1, ViewIndexDirAlmostFull == 1}.
+
+%% keeping the term small so it finds in a single machine word
+%% is apparently better.
+set_almost_full(false, false) ->
+    couch_log:notice("Removing database_dir and view_index_dir full flag", []),
+    persistent_term:erase(key());
+set_almost_full(true, false) ->
+    couch_log:warning("Setting database_dir full flag", []),
+    persistent_term:put(key(), <<1:1, 0:1>>);
+set_almost_full(false, true) ->
+    couch_log:warning("Setting view_index_dir full flag", []),
+    persistent_term:put(key(), <<0:1, 1:1>>);
+set_almost_full(true, true) ->
+    couch_log:warning("Setting database_dir and view_index_dir full flag", []),
+    persistent_term:put(key(), <<1:1, 1:1>>).
+
+key() ->
+    {?MODULE, disk_almost_full}.
diff --git a/src/couch/src/couch_app.erl b/src/couch/src/couch_app.erl
index 8cd8c8482..1887d1451 100644
--- a/src/couch/src/couch_app.erl
+++ b/src/couch/src/couch_app.erl
@@ -21,6 +21,9 @@
 ]).
 
 start(_Type, _) ->
+    %% register our alarm handler
+    gen_event:swap_handler(alarm_handler, {alarm_handler, swap}, {couch_alarm_handler, ok}),
+
     case couch_sup:start_link() of
         {ok, _} = Resp ->
             {Time, _} = statistics(wall_clock),
diff --git a/src/couch_mrview/src/couch_mrview.erl b/src/couch_mrview/src/couch_mrview.erl
index 1a4a3ebcc..a50fcd670 100644
--- a/src/couch_mrview/src/couch_mrview.erl
+++ b/src/couch_mrview/src/couch_mrview.erl
@@ -291,8 +291,8 @@ query_view(Db, DDoc, VName, Args0, Callback, Acc0) ->
                     _ -> {ok, Acc0}
                 end,
             query_view(Db, VInfo, Args, Callback, Acc1);
-        ddoc_updated ->
-            Callback(ok, ddoc_updated)
+        Error when Error == ddoc_updated; Error == insufficient_storage ->
+            Callback(ok, Error)
     end.
 
 get_view_index_pid(Db, DDoc, ViewName, Args0) ->
@@ -752,8 +752,8 @@ default_cb({final, Info}, []) ->
     {ok, [Info]};
 default_cb({final, _}, Acc) ->
     {ok, Acc};
-default_cb(ok, ddoc_updated) ->
-    {ok, ddoc_updated};
+default_cb(ok, Error) when Error == ddoc_updated; Error == insufficient_storage ->
+    {ok, Error};
 default_cb(Row, Acc) ->
     {ok, [Row | Acc]}.
 
diff --git a/src/couch_mrview/src/couch_mrview_util.erl b/src/couch_mrview/src/couch_mrview_util.erl
index 5913aa3d0..a6aa6edf0 100644
--- a/src/couch_mrview/src/couch_mrview_util.erl
+++ b/src/couch_mrview/src/couch_mrview_util.erl
@@ -158,8 +158,8 @@ get_view(Db, DDoc, ViewName, Args0) ->
             check_range(Args3, view_cmp(View)),
             Sig = view_sig(Db, State, View, Args3),
             {ok, {Type, View, Ref}, Sig, Args3};
-        ddoc_updated ->
-            ddoc_updated
+        Error when Error == ddoc_updated; Error == insufficient_storage ->
+            Error
     end.
 
 get_view_index_pid(Db, DDoc, ViewName, Args0) ->
@@ -176,6 +176,7 @@ get_view_index_state(_, DDoc, _, _, RetryCount) when RetryCount < 0 ->
     couch_log:warning("DDoc '~s' recreated too frequently", [DDoc#doc.id]),
     throw({get_view_state, exceeded_retry_count});
 get_view_index_state(Db, DDoc, ViewName, Args0, RetryCount) ->
+    ViewIndexDirAlmostFull = couch_alarm_handler:view_index_dir_almost_full(),
     try
         {ok, Pid, Args} = get_view_index_pid(Db, DDoc, ViewName, Args0),
         UpdateSeq = couch_util:with_db(Db, fun(WDb) ->
@@ -190,12 +191,24 @@ get_view_index_state(Db, DDoc, ViewName, Args0, RetryCount) ->
                     couch_index:get_state(Pid, 0);
                 false ->
                     couch_index:get_state(Pid, 0);
+                _ when ViewIndexDirAlmostFull ->
+                    %% if the view is already fresh enough, proceed.
+                    %% otherwise it's an error until the view index alarm clears.
+                    case couch_index:get_state(Pid, 0) of
+                        {ok, #mrst{update_seq = MaybeSeq} = St} when MaybeSeq >= UpdateSeq ->
+                            {ok, St};
+                        {ok, #mrst{}} ->
+                            insufficient_storage;
+                        Else0 ->
+                            Else0
+                    end;
                 _ ->
                     couch_index:get_state(Pid, UpdateSeq)
             end,
         case State of
             {ok, State0} -> {ok, State0, Args};
             ddoc_updated -> ddoc_updated;
+            insufficient_storage -> insufficient_storage;
             Else -> throw(Else)
         end
     catch
diff --git a/src/fabric/src/fabric_doc_update.erl b/src/fabric/src/fabric_doc_update.erl
index 77b424911..695ab07e9 100644
--- a/src/fabric/src/fabric_doc_update.erl
+++ b/src/fabric/src/fabric_doc_update.erl
@@ -112,6 +112,8 @@ handle_message({bad_request, Msg}, _, _) ->
     throw({bad_request, Msg});
 handle_message({forbidden, Msg}, _, _) ->
     throw({forbidden, Msg});
+handle_message({insufficient_storage, Msg}, _, _) ->
+    throw({insufficient_storage, Msg});
 handle_message({request_entity_too_large, Entity}, _, _) ->
     throw({request_entity_too_large, Entity}).
 
diff --git a/src/fabric/src/fabric_rpc.erl b/src/fabric/src/fabric_rpc.erl
index b781eea99..e47e98b6f 100644
--- a/src/fabric/src/fabric_rpc.erl
+++ b/src/fabric/src/fabric_rpc.erl
@@ -274,21 +274,27 @@ get_missing_revs(DbName, IdRevsList, Options) ->
     with_db(DbName, Options, {couch_db, get_missing_revs, [IdRevsList]}).
 
 update_docs(DbName, Docs0, Options) ->
-    {Docs1, Type} =
-        case couch_util:get_value(read_repair, Options) of
-            NodeRevs when is_list(NodeRevs) ->
-                Filtered = read_repair_filter(DbName, Docs0, NodeRevs, Options),
-                {Filtered, ?REPLICATED_CHANGES};
-            undefined ->
-                X =
-                    case proplists:get_value(?REPLICATED_CHANGES, Options) of
-                        true -> ?REPLICATED_CHANGES;
-                        _ -> ?INTERACTIVE_EDIT
-                    end,
-                {Docs0, X}
-        end,
-    Docs2 = make_att_readers(Docs1),
-    with_db(DbName, Options, {couch_db, update_docs, [Docs2, Options, Type]}).
+    %% only if there's room
+    case couch_alarm_handler:database_dir_almost_full() of
+        true ->
+            rexi:reply({insufficient_storage, <<"database_dir almost full">>});
+        false ->
+            {Docs1, Type} =
+                case couch_util:get_value(read_repair, Options) of
+                    NodeRevs when is_list(NodeRevs) ->
+                        Filtered = read_repair_filter(DbName, Docs0, NodeRevs, Options),
+                        {Filtered, ?REPLICATED_CHANGES};
+                    undefined ->
+                        X =
+                            case proplists:get_value(?REPLICATED_CHANGES, Options) of
+                                true -> ?REPLICATED_CHANGES;
+                                _ -> ?INTERACTIVE_EDIT
+                            end,
+                        {Docs0, X}
+                end,
+            Docs2 = make_att_readers(Docs1),
+            with_db(DbName, Options, {couch_db, update_docs, [Docs2, Options, Type]})
+    end.
 
 get_purge_seq(DbName, Options) ->
     with_db(DbName, Options, {couch_db, get_purge_seq, []}).
@@ -493,7 +499,9 @@ view_cb(complete, Acc) ->
     ok = rexi:stream_last(complete),
     {ok, Acc};
 view_cb(ok, ddoc_updated) ->
-    rexi:reply({ok, ddoc_updated}).
+    rexi:reply({ok, ddoc_updated});
+view_cb(ok, insufficient_storage) ->
+    rexi:reply({ok, insufficient_storage}).
 
 reduce_cb({meta, Meta}, Acc) ->
     % Map function starting
@@ -511,7 +519,9 @@ reduce_cb(complete, Acc) ->
     ok = rexi:stream_last(complete),
     {ok, Acc};
 reduce_cb(ok, ddoc_updated) ->
-    rexi:reply({ok, ddoc_updated}).
+    rexi:reply({ok, ddoc_updated});
+reduce_cb(ok, insufficient_storage) ->
+    rexi:reply({ok, insufficient_storage}).
 
 changes_enumerator(#full_doc_info{} = FDI, Acc) ->
     changes_enumerator(couch_doc:to_doc_info(FDI), Acc);
diff --git a/src/fabric/src/fabric_streams.erl b/src/fabric/src/fabric_streams.erl
index 2a3a2b004..318824814 100644
--- a/src/fabric/src/fabric_streams.erl
+++ b/src/fabric/src/fabric_streams.erl
@@ -144,11 +144,11 @@ handle_stream_start(rexi_STREAM_INIT, {Worker, From}, St) ->
                     {stop, St#stream_acc{workers = [], ready = Ready1}}
             end
     end;
-handle_stream_start({ok, ddoc_updated}, _, St) ->
+handle_stream_start({ok, Error}, _, St) when Error == ddoc_updated; Error == insufficient_storage ->
     WaitingWorkers = [W || {W, _} <- St#stream_acc.workers],
     ReadyWorkers = [W || {W, _} <- St#stream_acc.ready],
     cleanup(WaitingWorkers ++ ReadyWorkers),
-    {stop, ddoc_updated};
+    {stop, Error};
 handle_stream_start(Else, _, _) ->
     exit({invalid_stream_start, Else}).
 
diff --git a/src/fabric/src/fabric_view.erl b/src/fabric/src/fabric_view.erl
index 82f9c2bc6..15eb54353 100644
--- a/src/fabric/src/fabric_view.erl
+++ b/src/fabric/src/fabric_view.erl
@@ -415,7 +415,13 @@ maybe_update_others(
     ViewName,
     #mrargs{update = lazy} = Args
 ) ->
-    ShardsNeedUpdated = mem3:shards(DbName) -- ShardsInvolved,
+    ViewIndexDirAlmostFull = couch_alarm_handler:view_index_dir_almost_full(),
+    ShardsNeedUpdated = case ViewIndexDirAlmostFull of
+        true ->
+            [];
+        false ->
+            mem3:shards(DbName) -- ShardsInvolved
+    end,
     lists:foreach(
         fun(#shard{node = Node, name = ShardName}) ->
             rpc:cast(Node, fabric_rpc, update_mrview, [ShardName, DDoc, ViewName, Args])
diff --git a/src/fabric/src/fabric_view_map.erl b/src/fabric/src/fabric_view_map.erl
index 7b3b75863..b264ed5c4 100644
--- a/src/fabric/src/fabric_view_map.erl
+++ b/src/fabric/src/fabric_view_map.erl
@@ -56,6 +56,8 @@ go(Db, Options, DDoc, View, Args0, Callback, Acc, VInfo) ->
         of
             {ok, ddoc_updated} ->
                 Callback({error, ddoc_updated}, Acc);
+            {ok, insufficient_storage} ->
+                Callback({error, {insufficient_storage, <<"not enough room to update index">>}}, Acc);
             {ok, Workers} ->
                 try
                     go(DbName, Workers, VInfo, CoordArgs, Callback, Acc)
@@ -206,7 +208,7 @@ handle_message({execution_stats, _} = Msg, {_, From}, St) ->
     {Go, Acc} = Callback(Msg, AccIn),
     rexi:stream_ack(From),
     {Go, St#collector{user_acc = Acc}};
-handle_message(ddoc_updated, _Worker, State) ->
+handle_message(Error, _Worker, State) when Error == ddoc_updated; Error == insufficient_storage ->
     {stop, State}.
 
 merge_row(Dir, Collation, undefined, Row, Rows0) ->
diff --git a/src/fabric/src/fabric_view_reduce.erl b/src/fabric/src/fabric_view_reduce.erl
index 90fa523a1..d4d17d5e1 100644
--- a/src/fabric/src/fabric_view_reduce.erl
+++ b/src/fabric/src/fabric_view_reduce.erl
@@ -47,6 +47,8 @@ go(Db, DDoc, VName, Args, Callback, Acc, VInfo) ->
         of
             {ok, ddoc_updated} ->
                 Callback({error, ddoc_updated}, Acc);
+            {ok, insufficient_storage} ->
+                Callback({error, insufficient_storage}, Acc);
             {ok, Workers} ->
                 try
                     go2(DbName, Workers, VInfo, CoordArgs, Callback, Acc)
@@ -175,6 +177,8 @@ handle_message(complete, Worker, #collector{counters = Counters0} = State) ->
     C1 = fabric_dict:update_counter(Worker, 1, Counters0),
     fabric_view:maybe_send_row(State#collector{counters = C1});
 handle_message(ddoc_updated, _Worker, State) ->
+    {stop, State};
+handle_message(insufficient_storage, _Worker, State) ->
     {stop, State}.
 
 os_proc_needed(<<"_", _/binary>>) -> false;
diff --git a/src/ken/src/ken_server.erl b/src/ken/src/ken_server.erl
index 929ba47d7..5b8c3bb9f 100644
--- a/src/ken/src/ken_server.erl
+++ b/src/ken/src/ken_server.erl
@@ -487,6 +487,7 @@ should_start_job(#job{name = Name, seq = Seq, server = Pid}, State) ->
     IncrementalChannels = list_to_integer(config("incremental_channels", "80")),
     BatchChannels = list_to_integer(config("batch_channels", "20")),
     TotalChannels = IncrementalChannels + BatchChannels,
+    ViewIndexDirAlmostFull = couch_alarm_handler:view_index_dir_almost_full(),
     A = get_active_count(),
     #state{delay = Delay, batch_size = BS} = State,
     case ets:lookup(ken_workers, Name) of
@@ -506,7 +507,7 @@ should_start_job(#job{name = Name, seq = Seq, server = Pid}, State) ->
                             % spawn an index update.
                             {ok, MRSt} = couch_index:get_state(Pid, 0),
                             CurrentSeq = couch_mrview_index:get(update_seq, MRSt),
-                            (Seq - CurrentSeq) < Threshold;
+                            not ViewIndexDirAlmostFull andalso (Seq - CurrentSeq) < Threshold;
                         % Nouveau has three elements
                         {_, Index, nouveau} ->
                             nouveau_index_updater:outdated(Index);