You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by "nickva (via GitHub)" <gi...@apache.org> on 2025/07/30 19:47:10 UTC
[PR] Implement the ability to downgrade CouchDB versions [couchdb]
nickva opened a new pull request, #5611:
URL: https://github.com/apache/couchdb/pull/5611
Currently, CouchDB on-disk header structure allows appending new fields to the header record in such a way that newer versions can upgrade themselves from the old versions easily. However, it was not possible to perform a downgrade back to an old version in case something went wrong.
While in general it may not be safe to downgrade, it may be possible for some features so allow for such an option and make it configurable.
The release notes of future releases may indicate which version are downgrade safe. Then, to perform a downgrade users would enable the downgrade flag, downgrade, and then reset it back to default = false.
Implementation-wise, it's pretty basic, if the size of the new tuple is larger than the old one, only use as many tuple fields as that (now old version) knows about, everything else is discarded.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "iilyak (via GitHub)" <gi...@apache.org>.
iilyak commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2257130332
##########
src/couch/src/couch_bt_engine_header.erl:
##########
@@ -204,17 +204,16 @@ upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
upgrade_tuple(Old) when is_tuple(Old) ->
NewSize = record_info(size, db_header),
- if
- tuple_size(Old) < NewSize -> ok;
- true -> erlang:error({invalid_header_size, Old})
- end,
- {_, New} = lists:foldl(
- fun(Val, {Idx, Hdr}) ->
- {Idx + 1, setelement(Idx, Hdr, Val)}
+ Upgrade = tuple_size(Old) < NewSize,
+ ProhibitDowngrade = config:get_boolean("couchdb", "prohibit_downgrade", true),
+ OldKVs =
+ case {Upgrade, ProhibitDowngrade} of
+ {true, AnyBool} when is_boolean(AnyBool) -> tuple_to_list(Old);
+ {false, true} -> error({invalid_header_size, Old});
+ {false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
Review Comment:
Never mind the `Upgrade` is `false`, so it is either `==` or `>`.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva merged PR #5611:
URL: https://github.com/apache/couchdb/pull/5611
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2257353592
##########
rel/overlay/etc/default.ini:
##########
@@ -120,6 +120,16 @@ view_index_dir = {{view_index_dir}}
; this setting.
;cfile_skip_ioq = false
+; CouchDB will not normally allow a database downgrade to a previous version if
+; the new version added new fields to the database file header structure. You
+; can disable this protection by setting this to false. We recommend
+; re-enabling the protection after you have performed the downgrade but it is
+; not mandatory to do so.
+; Note that some future versions of CouchDB might not support downgrade at all,
+; whatever value this is set to. In those cases CouchDB will refuse to
+; downgrade or even open the databases in question.
+;prohibit_downgrade = true
Review Comment:
I did start that way actually, the pr branch is even named `allow-downgrades-option`, but there was a discussion in dev to improve wording and naming and I think we settled on having something that expresses the default as a `true` value. I am ok either way as well, but prohibit does sound a bit stronger and more worrisome which is a good thing in this case.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2250054241
##########
src/couch/src/couch_bt_engine_header.erl:
##########
@@ -204,17 +204,16 @@ upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
upgrade_tuple(Old) when is_tuple(Old) ->
NewSize = record_info(size, db_header),
- if
- tuple_size(Old) < NewSize -> ok;
- true -> erlang:error({invalid_header_size, Old})
- end,
- {_, New} = lists:foldl(
- fun(Val, {Idx, Hdr}) ->
- {Idx + 1, setelement(Idx, Hdr, Val)}
+ Upgrade = tuple_size(Old) < NewSize,
+ ProhibitDowngrade = config:get_boolean("couchdb", "prohibit_downgrade", true),
+ OldKVs =
+ case {Upgrade, ProhibitDowngrade} of
+ {true, AnyBool} when is_boolean(AnyBool) -> tuple_to_list(Old);
+ {false, true} -> error({invalid_header_size, Old});
+ {false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
Review Comment:
Updated the comment to emphasize it’s for matching disk version numbers only
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2248680667
##########
src/couch/src/couch_bt_engine_header.erl:
##########
@@ -204,17 +204,16 @@ upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
upgrade_tuple(Old) when is_tuple(Old) ->
NewSize = record_info(size, db_header),
- if
- tuple_size(Old) < NewSize -> ok;
- true -> erlang:error({invalid_header_size, Old})
- end,
- {_, New} = lists:foldl(
- fun(Val, {Idx, Hdr}) ->
- {Idx + 1, setelement(Idx, Hdr, Val)}
+ Upgrade = tuple_size(Old) < NewSize,
+ ProhibitDowngrade = config:get_boolean("couchdb", "prohibit_downgrade", true),
+ OldKVs =
+ case {Upgrade, ProhibitDowngrade} of
+ {true, AnyBool} when is_boolean(AnyBool) -> tuple_to_list(Old);
+ {false, true} -> error({invalid_header_size, Old});
+ {false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
Review Comment:
Good point, currently this is only meant to work as a counter-part to
```
% As long the changes are limited to new header fields (with inline
% defaults) added to the end of the record, then there is no need to increment
% the disk revision number.
```
In other words this doesn't apply when version changes. It only applies when the version matches and it's fields appended to the end.
> The downgrade in general is not solvable problem if we don't maintain fields order for a previous version.
Agree, that's why we don't attempt to solve a more general problem just a specific corner case.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2248744813
##########
src/couch/src/couch_bt_engine_header.erl:
##########
@@ -204,17 +204,16 @@ upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
upgrade_tuple(Old) when is_tuple(Old) ->
NewSize = record_info(size, db_header),
- if
- tuple_size(Old) < NewSize -> ok;
- true -> erlang:error({invalid_header_size, Old})
- end,
- {_, New} = lists:foldl(
- fun(Val, {Idx, Hdr}) ->
- {Idx + 1, setelement(Idx, Hdr, Val)}
+ Upgrade = tuple_size(Old) < NewSize,
+ ProhibitDowngrade = config:get_boolean("couchdb", "prohibit_downgrade", true),
+ OldKVs =
+ case {Upgrade, ProhibitDowngrade} of
+ {true, AnyBool} when is_boolean(AnyBool) -> tuple_to_list(Old);
+ {false, true} -> error({invalid_header_size, Old});
+ {false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
Review Comment:
I'll update the commit comment, as it was missing the part that is only meant for cases when the disk version hasn't changed and is meant to work along with the feature were we allowed "easy" upgrades by just appending fields to the header
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "iilyak (via GitHub)" <gi...@apache.org>.
iilyak commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2248619020
##########
src/couch/src/couch_bt_engine_header.erl:
##########
@@ -204,17 +204,16 @@ upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
upgrade_tuple(Old) when is_tuple(Old) ->
NewSize = record_info(size, db_header),
- if
- tuple_size(Old) < NewSize -> ok;
- true -> erlang:error({invalid_header_size, Old})
- end,
- {_, New} = lists:foldl(
- fun(Val, {Idx, Hdr}) ->
- {Idx + 1, setelement(Idx, Hdr, Val)}
+ Upgrade = tuple_size(Old) < NewSize,
+ ProhibitDowngrade = config:get_boolean("couchdb", "prohibit_downgrade", true),
+ OldKVs =
+ case {Upgrade, ProhibitDowngrade} of
+ {true, AnyBool} when is_boolean(AnyBool) -> tuple_to_list(Old);
+ {false, true} -> error({invalid_header_size, Old});
+ {false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
Review Comment:
I think this only works if field was added at the end. How about the case when we remove one of the fields in the middle? The downgrade in general is not solvable problem if we don't maintain fields order for a previous version. We can try something like:
```
record_info(1) ->
[
disk_version, update_seq, ...
];
...
record_info(7) ->
[
disk_version, update_seq, unused, id_tree_state,
seq_tree_state, local_tree_state, purge_tree_state,
purge_seq_tree_state, security_ptr, revs_limit, uuid,
epochs, compacted_seq, purge_infos_limit, props_ptr
];
record_info(?LATEST_DISK_VERSION) ->
record_info(fields, db_header).
```
If we would maintain such info we should be able to figure out what has been changed.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2248680667
##########
src/couch/src/couch_bt_engine_header.erl:
##########
@@ -204,17 +204,16 @@ upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
upgrade_tuple(Old) when is_tuple(Old) ->
NewSize = record_info(size, db_header),
- if
- tuple_size(Old) < NewSize -> ok;
- true -> erlang:error({invalid_header_size, Old})
- end,
- {_, New} = lists:foldl(
- fun(Val, {Idx, Hdr}) ->
- {Idx + 1, setelement(Idx, Hdr, Val)}
+ Upgrade = tuple_size(Old) < NewSize,
+ ProhibitDowngrade = config:get_boolean("couchdb", "prohibit_downgrade", true),
+ OldKVs =
+ case {Upgrade, ProhibitDowngrade} of
+ {true, AnyBool} when is_boolean(AnyBool) -> tuple_to_list(Old);
+ {false, true} -> error({invalid_header_size, Old});
+ {false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
Review Comment:
Good point, currently this is only meant to work as a counterpart to
```
% As long the changes are limited to new header fields (with inline
% defaults) added to the end of the record, then there is no need to increment
% the disk revision number.
```
In other words, this doesn't apply when disk version changes. It only applies when the disk version matches and it's just filed fields appended to the end of the header.
> The downgrade in general is not solvable problem if we don't maintain fields order for a previous version.
Agree, that's why we don't attempt to solve a more general problem just a specific corner case.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "iilyak (via GitHub)" <gi...@apache.org>.
iilyak commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2257086153
##########
src/couch/src/couch_bt_engine_header.erl:
##########
@@ -204,17 +204,16 @@ upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
upgrade_tuple(Old) when is_tuple(Old) ->
NewSize = record_info(size, db_header),
- if
- tuple_size(Old) < NewSize -> ok;
- true -> erlang:error({invalid_header_size, Old})
- end,
- {_, New} = lists:foldl(
- fun(Val, {Idx, Hdr}) ->
- {Idx + 1, setelement(Idx, Hdr, Val)}
+ Upgrade = tuple_size(Old) < NewSize,
+ ProhibitDowngrade = config:get_boolean("couchdb", "prohibit_downgrade", true),
+ OldKVs =
+ case {Upgrade, ProhibitDowngrade} of
+ {true, AnyBool} when is_boolean(AnyBool) -> tuple_to_list(Old);
+ {false, true} -> error({invalid_header_size, Old});
+ {false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
Review Comment:
The `NewSize` is greater than the size of the tuple, which means the `lists:sublist` invocation would return the same tuple.
```erlang
Upgrade = tuple_size(Old) < NewSize,
...
{false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "iilyak (via GitHub)" <gi...@apache.org>.
iilyak commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2257142480
##########
rel/overlay/etc/default.ini:
##########
@@ -120,6 +120,16 @@ view_index_dir = {{view_index_dir}}
; this setting.
;cfile_skip_ioq = false
+; CouchDB will not normally allow a database downgrade to a previous version if
+; the new version added new fields to the database file header structure. You
+; can disable this protection by setting this to false. We recommend
+; re-enabling the protection after you have performed the downgrade but it is
+; not mandatory to do so.
+; Note that some future versions of CouchDB might not support downgrade at all,
+; whatever value this is set to. In those cases CouchDB will refuse to
+; downgrade or even open the databases in question.
+;prohibit_downgrade = true
Review Comment:
did you consider using positive logic?
```
; allow_downgrade = false
```
I don't mind either way.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2257353592
##########
rel/overlay/etc/default.ini:
##########
@@ -120,6 +120,16 @@ view_index_dir = {{view_index_dir}}
; this setting.
;cfile_skip_ioq = false
+; CouchDB will not normally allow a database downgrade to a previous version if
+; the new version added new fields to the database file header structure. You
+; can disable this protection by setting this to false. We recommend
+; re-enabling the protection after you have performed the downgrade but it is
+; not mandatory to do so.
+; Note that some future versions of CouchDB might not support downgrade at all,
+; whatever value this is set to. In those cases CouchDB will refuse to
+; downgrade or even open the databases in question.
+;prohibit_downgrade = true
Review Comment:
I did start that way actually, the pr branch is actually named `allow-downgrades-option`, but there was a discussion in dev to improve wording and naming and I think we settled on having something that expresses the default as a `true` value. I am ok either way as well, but prohibit does sound a bit stronger and more worrisome which is a good thing in this case.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2257368905
##########
src/couch/src/couch_bt_engine_header.erl:
##########
@@ -204,17 +204,16 @@ upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
upgrade_tuple(Old) when is_tuple(Old) ->
NewSize = record_info(size, db_header),
- if
- tuple_size(Old) < NewSize -> ok;
- true -> erlang:error({invalid_header_size, Old})
- end,
- {_, New} = lists:foldl(
- fun(Val, {Idx, Hdr}) ->
- {Idx + 1, setelement(Idx, Hdr, Val)}
+ Upgrade = tuple_size(Old) < NewSize,
+ ProhibitDowngrade = config:get_boolean("couchdb", "prohibit_downgrade", true),
+ OldKVs =
+ case {Upgrade, ProhibitDowngrade} of
+ {true, AnyBool} when is_boolean(AnyBool) -> tuple_to_list(Old);
+ {false, true} -> error({invalid_header_size, Old});
+ {false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
Review Comment:
yeah, and we had handled the `==` as well anyway in the no-op clause of the function:
```
upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
```
So only `>` remains and that seems to work like:
```
> lists:sublist(tuple_to_list({rec,a,b}), 2).
[rec,a]
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement the ability to downgrade CouchDB versions even on header field changes [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva commented on code in PR #5611:
URL: https://github.com/apache/couchdb/pull/5611#discussion_r2248680667
##########
src/couch/src/couch_bt_engine_header.erl:
##########
@@ -204,17 +204,16 @@ upgrade_tuple(Old) when is_record(Old, db_header) ->
Old;
upgrade_tuple(Old) when is_tuple(Old) ->
NewSize = record_info(size, db_header),
- if
- tuple_size(Old) < NewSize -> ok;
- true -> erlang:error({invalid_header_size, Old})
- end,
- {_, New} = lists:foldl(
- fun(Val, {Idx, Hdr}) ->
- {Idx + 1, setelement(Idx, Hdr, Val)}
+ Upgrade = tuple_size(Old) < NewSize,
+ ProhibitDowngrade = config:get_boolean("couchdb", "prohibit_downgrade", true),
+ OldKVs =
+ case {Upgrade, ProhibitDowngrade} of
+ {true, AnyBool} when is_boolean(AnyBool) -> tuple_to_list(Old);
+ {false, true} -> error({invalid_header_size, Old});
+ {false, false} -> lists:sublist(tuple_to_list(Old), NewSize)
Review Comment:
Good point, currently this is only meant to work as a counter-part to
```
% As long the changes are limited to new header fields (with inline
% defaults) added to the end of the record, then there is no need to increment
% the disk revision number.
```
In other words this doesn't apply when version changes. It only applies when the version matches and it's fields appended to the end.
> The downgrade in general is not solvable problem if we don't maintain fields order for a previous version.
Exactly, so we don't attempt to solve a more general problem just a specific corner case.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org