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

Calculate ipset maxelem instead of hardcoding #20

Merged
merged 2 commits into from
Jan 1, 2022
Merged

Calculate ipset maxelem instead of hardcoding #20

merged 2 commits into from
Jan 1, 2022

Conversation

johnmave126
Copy link
Contributor

The Maxmind US.ipv6 set contains more than 131072 entries as of now which will raise errors when trying to import. This PR counts the number of entry first and rounds up to the next next power of 2 to avoid allocating an ipset that is too small.

@@ -160,7 +168,9 @@ def build_sets(self, country_code_map: dict, zip_ref: ZipFile, dir_prefix: str,
ipset_file = ipset_dir / set_name
if not ipset_file.is_file():
with open(ipset_file, 'a') as f:
f.write("create " + set_name + " hash:net " + inet_family + " maxelem 131072 comment\n")
# round up to the next next power of 2 for a load ratio < 0.5
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure maxelem has any bearing on the actual size of the underlying hash data structure used by ipset -- and therefore any effect on the load ratio of the hash table. It is simply a cap on the number of elements in a set.

There is a separate hashsize setting that controls the initial size of the hash table, but the actual size is managed internally by ipset based on the number of set elements at any given time. (According to the manpage the size is doubled if existing size is exhausted but there is probably more to it than that.)

Given this, I think setting maxelem to the next power of 2 (instead of the next next power of 2) is sufficient. If you agree, please push that change to the PR. If I've missed something, though, please do let me know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are absolutely right. Now begging the question: Do we set the exact maxelem since it doesn't need to be a power of 2
according to the man page?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick with a power of 2. The default (65536) and example (2048) for maxelem in the manpage are both a power of 2 and doing otherwise just feels wrong.

@chr0mag
Copy link
Owner

chr0mag commented Dec 30, 2021

Hi @johnmave126 - thanks for this enhancement. I've added one note to the PR and will merge once that's resolved.

There are a few other fixes and enhancements on the way, so I will wait for those to accumulate before cutting a new release that includes this PR.

@chr0mag chr0mag merged commit 946ca82 into chr0mag:main Jan 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants