🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #6679

Storage of Usenet News Articles by MD5 Hash

#6679
From: Scott Lystig Fritchie
Date: Wed, 17 Apr 1996 00:00
376 lines
14867 bytes
Storage of Usenet News Articles by MD5 Hash
===========================================

This idea originally came from Jerry Aguirre
<jerry@strobe.ATC.Olivetti.Com> on Leap Day 1996.  In message
<4gl99l$blm@olivea.ATC.Olivetti.Com>, he suggested:

	If your going to change that much then why not convert to my favorite
	idea.  Store the articles by message ID instead of using that damn
	stupid article number that is purely arbitrary and doesn't port from
	system to system.  To do this efficiently one would still need some
	kind of hash and subdirectory scheme to keep the sizes practical.  But
	one could divide it evenly based on the total number of articles and
	not on the vagaries of each news group's volume.  If one hashed the
	message ID so each directory held 127 entries one could store 2 million
	article in 3 small levels!

There has been a lot of discussion in news.software.nntp since then
about creating the ultimate Usenet News "filesystem".  I thought I'd
take my first stab at it.

It Isn't Ultimate, But...
=========================

It wasn't meant to be.  I just wanted to see how difficult it would be
to implement Jerry's idea.

	NOTA BENE: MRNet has got several Usenet feeder machines which
		in turn feed MRNet member sites.  I developed this
		hack for those machines.  Since they do *not* support
		newsreaders, I didn't bother solving the problem
		of how to allow traditional newsreaders the ability
		to read news from a server storing articles using
		MD5 hashes.

T. William Wells <bill@twwells.com> said in <Dn4HrC.KIx@twwells.com>
on 21 Feb 1996 guesstimated that a typical Usenet News server ends up
reading about 64Kbytes worth of data just to store a single news
article.  Why?  With the various levels of subdirectories in group
names such as alt.adjective.noun.verb.verb.verb, then finding an empty
slot to put the articlenumber/filename into, can take a lot of
reading, which in turn is spread across several disk head seeks.
Wasteful, as most everyone wholeheartedly agrees.

INND will normally store an article in a file named by replacing the
newsgroup name's dots with slashes to get the subdirectory name, then
appending the article number for the filename.  My approach:

	1. Create an MD5 hash of the article's "Message-ID".
	2. Use 2^6 bits in the first byte as the first subdirectory
	   name.
	3. Use 2^6 bits in the second byte as the second subdirectory
	   name.
	4. Use the next 4 bytes for the filename.

For example, an article with the Message-ID
<4l0u5t$ljs@news.hal-pc.org> would be stored using the path
"01/03/bf9ca883".

Given an incoming load of 180K articles/day, you get roughly 3 days
worth of storage.  (If these feeder machines don't get an article to a
member site in 2 or 3 days, it probably won't ever get there.)  With
two levels of 2^6 (64) subdirectories each, you'd have about 128 files
per subdirectory.  Each filename would be 8 bytes long, which would
mean the largest directory namei() would have to scan is about 2K.

Astute readers would note that 64 bits of MD5 hash isn't enough to
make sufficiently unlikely the event of a filename collision.  So, I
fudged and inserted a stat() loop to check to see if a file by that
name already exists; if so, append a "+" sign to the name and try again.

Jerry's proposal was: "If one hashed the message ID so each directory
held 127 entries one could store 2 million article in 3 small levels!"

Benefits
========

Aside from the benefit of less reading when searching for or creating
a file, there's one other that Jerry mentioned in correspondence with
me: a benefit for those folks using OSes which don't support a
"metadisk" or "logical volume" and must resort to using multiple
filesystems and symlinks pointing to various directory hierarchies.
Since we hash by Message-ID instead of group name, and because each
byte of an MD5 hash is roughly equally likely to be any value between
0x00 and 0xff, you can very evenly distribute the article load across
several disks ... and be reasonably certain that you won't have
unwanted clumps of articles on one disk.

Drawbacks
=========

See the "Nota Bene" above.  If you didn't read it, the main drawback
is that I didn't develop the code for the overview database (and other
tidbits) to support newsreaders.  I was not faced with that problem,
so I didn't try solving it.

Also, this method plays havoc with expiration policies that aren't of
the "all articles get removed after N days".  {shrug}  Another
exercise for the reader.

Installation Instructions
=========================

-1. This isn't production code.  It works on my machine under Solaris
2.5 (and under FreeBSD 2.1, with a bit of massaging).  If it doesn't
work for you, work on it some more.  The solution will come to you. :-)

0. Make a copy of the INN 1.4 source tree, including your
config/config.data file.

If you want to use something more recent, like INN 1.4unoff4, I'm sure
you won't have too much difficulty dealing with the patches.

1. Extract the uuencode'd MD5 compressed tar file into the top INN
top-level source tree directory.  (i.e. you should see "doc", "innd",
and "sigs" all in the same directory)

This tar file, since it's a little on the big side, can be retrieved
via http://www.mr.net/~fritchie/HASHHACK/sigs.tar.Z.  If you can't
retrieve it, lemme know and I'll email you a uuencoded version.

Please note that this MD5 source code is borrowed from Tripwire, a
wonderful utility by Gene Spafford (?) and Gene Kim.  They in turn
borrowed code from RSA Data Security, Inc.  Please note and respect
the MD5 source code's copyright statement:

	Copyright (C) 1990, RSA Data Security, Inc. All rights
	reserved.

Please see sigs/md5/README.md5 for full licensing agreement text.

2. "cd sigs/md5".  Edit the first #include statement in
../include/config.h.  If you're unfamiliar with that to do, look at
the documentation for Tripwire.  :-)

3. "make".  Then "./md5 /etc/motd" should spit out a list of
hexadecimal'ified lines like:

	41 ae 13 48 0c 39 c2 29 9b 03 99 7c 8d 63 d5 39
	03 89 f7 3c 26 29 ad b9 f6 3a d4 e8 46 27 76 67

4. "cd ../.."

5. Apply the HASHHACK patch using "patch < /path/to/HASHHACK.patch".
You'll probably run into problems with a couple of the patches to
Makefiles.  Find the rejected stub files and apply them by hand.
(They're small, so it won't hurt ... much.)

5b. The patch to config/config.data probably didn't work, either.  The
only thing to do by hand is the CFLAGS line.  The "MRNET_HACK" symbol
is optional -- it's to work around a select() bug in Solaris 2.4 (and
Solaris 2.5 multi-processor boxes).

6. Install just as you normally would install INN.  "make install" if
you're doing it for the first time, or "make update" to avoid
clobbering all of your local config files in a previous INN
installation.

7. Start up "innd" with your handy-dandy "rc.news" script.

8. To avoid *really* huge daily reports, include "flags=-q" on your daily
report "crontab" entry, e.g.

1 0 * * * /var/news/bin/news.daily delayrm flags=-q
# ... or ...
1 8,16 * * * /var/news/bin/news.daily delayrm nologs norotate nomail flags=-q

9. Enjoy!  I probably left something out, but you've probably discovered
that by now.  {sigh}

10. This software wouldn't have happened without the constant prodding
by one of my colleagues, Ben Black <black@mr.net>.  He has cooked up a
Web-retrievable doo-bitty which contains this information and more.
See http://www.mr.net/~black/newshacks.html.  (Kitsch, no?)  Or see
http://www.mr.net/~fritchie/HASHHACK/.

-Scott
---
Scott Lystig Fritchie, Network Engineer          Minnesota Regional Network
fritchie@mr.net, PGP key #152B8725               2829 University Ave SE
v: 612/362.5820, p: 612/637.9547                 Minneapolis, MN  55414
http://www.mr.net/~fritchie/

--- snip --- snip --- snip --- snip --- snip --- snip --- snip --- snip --- 

*** ./config/config.data	Wed Jan 31 10:00:18 1996
--- ../inn1.4-HASHHACK/config/config.data	Tue Apr 16 23:18:32 1996
***************
*** 18,30 ****
  DEFS			-I../include -DSUNOS5
  ##  C compiler
  #### =()<CC			@<CC>@>()=
! CC			cc
  ##  Does your compiler properly do "char const *"? Pick DO DONT or DUNNO
  #### =()<USE_CHAR_CONST		@<USE_CHAR_CONST>@>()=
  USE_CHAR_CONST		DO
  ##  C compiler flags
  #### =()<CFLAGS			@<CFLAGS>@>()=
! CFLAGS			$(DEFS)
  ##  C compiler flags to use when compiling dbz
  #### =()<DBZCFLAGS			@<DBZCFLAGS>@>()=
  DBZCFLAGS			$(CFLAGS)
--- 18,30 ----
  DEFS			-I../include -DSUNOS5
  ##  C compiler
  #### =()<CC			@<CC>@>()=
! CC			gcc
  ##  Does your compiler properly do "char const *"? Pick DO DONT or DUNNO
  #### =()<USE_CHAR_CONST		@<USE_CHAR_CONST>@>()=
  USE_CHAR_CONST		DO
  ##  C compiler flags
  #### =()<CFLAGS			@<CFLAGS>@>()=
! CFLAGS			$(DEFS) -DMRNET_HACK -DXXX_HASHHACK
  ##  C compiler flags to use when compiling dbz
  #### =()<DBZCFLAGS			@<DBZCFLAGS>@>()=
  DBZCFLAGS			$(CFLAGS)
*** ./innd/Makefile	Wed Jan 31 10:00:46 1996
--- ../inn1.4-HASHHACK/innd/Makefile	Tue Apr 16 23:45:14 1996
***************
*** 30,35 ****
--- 30,37 ----
  
  ##  =()<LIBS	= @<LIBS>@>()=
+ LIBS	= -lnsl -lsocket -lelf -lresolv ../sigs/md5/md5.o \
+ 		../sigs/md5/md5wrapper.o
  LIBNEWS	= ../libinn.a
  LINTLIB	= ../llib-linn.ln
  

*** ./innd/art.c	Thu Mar 18 15:04:21 1993
--- ../inn1.4-HASHHACK/innd/art.c	Wed Mar  6 12:02:26 1996
***************
*** 1991,1999 ****
--- 1990,2032 ----
  
  	if (Data.Name[0] == '\0') {
  	    /* Write the article the first time. */
+ #ifdef XXX_HASHHACK
+ 	    /*
+ 	    ** We'll try for 2 subdirs of 2^6 dirs each.  For 512Karticles,
+ 	    ** that would result in a max average of 128 articles per
+ 	    ** directory.  (2^19 - 2^12 = 2^7 = 128)
+ 	    ** MRNet's current news flow is 180Karticles/day, which means
+ 	    ** 512Karticles is about 3 days worth, more than enough space for
+ 	    ** feeder machines.
+ 	    */
+ 	    int sig[16];
+ 	    char hasheddirname[32];
+ 	    struct stat statbuf;	/* Not used, really */
+ 	    int mask = 64 - 1;	/* 2^6 - 1, a mask for 6 bits worth of info */
+ 
+ 	    sig_md5_get(Data.MessageID, sig);
+ 	    sprintf(hasheddirname, "%02x/%02x", sig[0] & mask, sig[1] & mask);
+ 	    sprintf(Data.Name, "%s/%02x%02x%02x%02x", hasheddirname,
+ 		    sig[2], sig[3], sig[4], sig[5]);
+ 	    /*
+ 	    ** There's a small chance for collision.  If so, fix it by
+ 	    ** appending something.  The extra stat will cause the destination
+ 	    ** directories to be read, something which must happen anyway
+ 	    ** if we're going to create a file.  {shrug}  This shouldn't be
+ 	    ** a big loss strategy....
+ 	    */
+ 	    while (stat(Data.Name, &statbuf) == 0) {
+ 		strcat(Data.Name, "+");
+ 	    }
+ #else /* XXX_HASHHACK */
  	    (void)sprintf(Data.Name, "%s/%lu", ngp->Dir, ngp->Filenum);
+ #endif /* XXX_HASHHACK */
  	    if (ARTwrite(Data.Name, article, &Data, CrossPosted) < 0
+ #ifdef XXX_HASHHACK
+ 	     && (!MakeSpoolDirectory(hasheddirname)
+ #else /* XXX_HASHHACK */
  	     && (!MakeSpoolDirectory(ngp->Dir)
+ #endif /* XXX_HASHHACK */
  	      || ARTwrite(Data.Name, article, &Data, CrossPosted) < 0)) {
  		syslog(L_ERROR, "%s cant write %s %m", LogName, Data.Name);
  		ARTlog(&Data, ART_REJECT, errNOSPACE);
***************
*** 2006,2011 ****
--- 2039,2045 ----
  	    Data.NameLength = strlen(Data.Name);
  	}
  	else {
+ #ifndef XXX_HASHHACK
  	    /* Link to the main article. */
  	    (void)sprintf(linkname, "%s/%lu", ngp->Dir, ngp->Filenum);
  	    if (link(Data.Name, linkname) < 0
***************
*** 2031,2036 ****
--- 2065,2071 ----
  	    }
  	    *p++ = ' ';
  	    p += strlen(strcpy(p, linkname));
+ #endif /* ! XXX_HASHHACK */
  	}
      }
  
*** ./innd/chan.c	Sun Apr  2 19:23:49 1995
--- ../inn1.4-HASHHACK/innd/chan.c	Wed Jan 31 17:12:33 1996
***************
*** 497,504 ****
      bp->Left = bp->Size - bp->Used;
      i = read(cp->fd, &bp->Data[bp->Used], bp->Left - 1);
      if (i < 0) {
! 	syslog(L_ERROR, "%s cant read %m", p);
  	return -1;
      }
      if (i == 0) {
  	syslog(L_NOTICE, "%s readclose", p);
--- 497,520 ----
      bp->Left = bp->Size - bp->Used;
      i = read(cp->fd, &bp->Data[bp->Used], bp->Left - 1);
      if (i < 0) {
! #ifdef	MRNET_HACK
!         int retries = 0;
! 
! 	if (errno == EAGAIN) {
! 	  while (retries++ < 10 && i < 0 && errno == EAGAIN) {
! 	    /* syslog(L_ERROR, "%s got EAGAIN, retrying %d", p, retries); */
! 	    sleep(1);
! 	    i = read(cp->fd, &bp->Data[bp->Used], bp->Left - 1);
! 	  }
! 	}
! 	if (i < 0) {
! 	  syslog(L_ERROR, "%s cant read (i=%d) %m", p, i);
! 	  return -1;
! 	}
! #else	/* MRNET_HACK */
! 	syslog(L_ERROR, "%s cant read %m", p, i);
  	return -1;
+ #endif	/* MRNET_HACK */
      }
      if (i == 0) {
  	syslog(L_NOTICE, "%s readclose", p);
*** ./innd/nc.c	Sun Apr  2 19:23:59 1995
--- ../inn1.4-HASHHACK/innd/nc.c	Tue Mar  5 12:28:35 1996
***************
*** 961,967 ****
      if ((p = GetConfigValue(_CONF_PATHHOST)) == NULL)
  	/* Worked in main, now it fails?  Curious. */
  	p = Path.Data;
!     (void)sprintf(buff, "%d %s InterNetNews server %s ready",
  	    NNTP_POSTOK_VAL, p, Version);
      NCgreeting = COPY(buff);
  
--- 961,967 ----
      if ((p = GetConfigValue(_CONF_PATHHOST)) == NULL)
  	/* Worked in main, now it fails?  Curious. */
  	p = Path.Data;
!     (void)sprintf(buff, "%d %s InterNetNews HASHHACK server %s ready",
  	    NNTP_POSTOK_VAL, p, Version);
      NCgreeting = COPY(buff);
  
*** ./innd/ng.c	Thu Mar 18 15:04:30 1993
--- ../inn1.4-HASHHACK/innd/ng.c	Wed Mar  6 11:59:57 1996
***************
*** 318,323 ****
--- 318,324 ----
  NGrenumber(ngp)
      NEWSGROUP		*ngp;
  {
+ #ifndef XXX_HASHHACK
      static char		NORENUMBER[] = "%s cant renumber %s %s too wide";
      static char		RENUMBER[] = "%s renumber %s %s from %ld to %ld";
      register DIR	*dp;
***************
*** 384,388 ****
--- 385,390 ----
  	}
  	ICDactivedirty++;
      }
+ #endif /* XXX_HASHHACK */
      return TRUE;
  }

Technical Details
Message-ID: <ytlenpniaty.fsf@datadata.mr.net>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail

Show Full Headers
Complete Article Headers
Message-ID: <ytlenpniaty.fsf@datadata.mr.net>
Subject: Storage of Usenet News Articles by MD5 Hash
Date: Wed, 17 Apr 1996 00:00:00 +0000
From: fritchie@MR.Net (Scott Lystig Fritchie)
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail
X-Deja-AN: 147932305
Distribution: inet
Sender: fritchie@data
Organization: Minnesota Regional Network
Newsgroups: news.software.nntp