chiark / gitweb /
Bias up recently added tracks. See new_bias and new_bias_age in
[disorder] / server / choose.c
1 /*
2  * This file is part of DisOrder 
3  * Copyright (C) 2008 Richard Kettlewell
4  * Copyright (C) 2008 Mark Wooding
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */
21 /** @file choose.c
22  * @brief Random track chooser
23  *
24  * Picks a track at random and writes it to standard output.  If for
25  * any reason no track can be picked - even a trivial reason like a
26  * deadlock - it just exits and expects the server to try again.
27  */
28
29 #include <config.h>
30 #include "types.h"
31
32 #include <getopt.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <db.h>
36 #include <locale.h>
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <pcre.h>
41 #include <string.h>
42 #include <fcntl.h>
43 #include <syslog.h>
44 #include <time.h>
45
46 #include "configuration.h"
47 #include "log.h"
48 #include "defs.h"
49 #include "mem.h"
50 #include "kvp.h"
51 #include "syscalls.h"
52 #include "printf.h"
53 #include "trackdb.h"
54 #include "trackdb-int.h"
55 #include "version.h"
56 #include "trackname.h"
57 #include "queue.h"
58 #include "server-queue.h"
59
60 #define BASE_WEIGHT 90000
61
62 static DB_TXN *global_tid;
63
64 static const struct option options[] = {
65   { "help", no_argument, 0, 'h' },
66   { "version", no_argument, 0, 'V' },
67   { "config", required_argument, 0, 'c' },
68   { "debug", no_argument, 0, 'd' },
69   { "no-debug", no_argument, 0, 'D' },
70   { "syslog", no_argument, 0, 's' },
71   { "no-syslog", no_argument, 0, 'S' },
72   { 0, 0, 0, 0 }
73 };
74
75 /* display usage message and terminate */
76 static void help(void) {
77   xprintf("Usage:\n"
78           "  disorder-choose [OPTIONS]\n"
79           "Options:\n"
80           "  --help, -h              Display usage message\n"
81           "  --version, -V           Display version number\n"
82           "  --config PATH, -c PATH  Set configuration file\n"
83           "  --debug, -d             Turn on debugging\n"
84           "  --[no-]syslog           Enable/disable logging to syslog\n"
85           "\n"
86           "Track choose for DisOrder.  Not intended to be run\n"
87           "directly.\n");
88   xfclose(stdout);
89   exit(0);
90 }
91 /** @brief Sum of all weights */
92 static unsigned long long total_weight;
93
94 /** @brief The winning track */
95 static const char *winning = 0;
96
97 /** @brief Count of tracks */
98 static long ntracks;
99
100 static char **required_tags;
101 static char **prohibited_tags;
102
103 static int queue_contains(const struct queue_entry *head,
104                           const char *track) {
105   const struct queue_entry *q;
106
107   for(q = head->next; q != head; q = q->next)
108     if(!strcmp(q->track, track))
109       return 1;
110   return 0;
111 }
112
113 /** @brief Compute the weight of a track
114  * @param track Track name (UTF-8)
115  * @param data Track data
116  * @param prefs Track preferences
117  * @return Track weight (non-negative)
118  *
119  * Tracks to be excluded entirely are given a weight of 0.
120  */
121 static unsigned long compute_weight(const char *track,
122                                     struct kvp *data,
123                                     struct kvp *prefs) {
124   const char *s;
125   char **track_tags;
126   time_t last, now = time(0);
127
128   /* Reject tracks not in any collection (race between edit config and
129    * rescan) */
130   if(!find_track_root(track)) {
131     info("found track not in any collection: %s", track);
132     return 0;
133   }
134
135   /* Reject aliases to avoid giving aliased tracks extra weight */
136   if(kvp_get(data, "_alias_for"))
137     return 0;
138   
139   /* Reject tracks with random play disabled */
140   if((s = kvp_get(prefs, "pick_at_random"))
141      && !strcmp(s, "0"))
142     return 0;
143
144   /* Reject tracks played within the last 8 hours */
145   if((s = kvp_get(prefs, "played_time"))) {
146     last = atoll(s);
147     if(now < last + config->replay_min)
148       return 0;
149   }
150
151   /* Reject tracks currently in the queue or in the recent list */
152   if(queue_contains(&qhead, track)
153      || queue_contains(&phead, track))
154     return 0;
155
156   /* We'll need tags for a number of things */
157   track_tags = parsetags(kvp_get(prefs, "tags"));
158
159   /* Reject tracks with prohibited tags */
160   if(prohibited_tags && tag_intersection(track_tags, prohibited_tags))
161     return 0;
162
163   /* Reject tracks that lack required tags */
164   if(*required_tags && !tag_intersection(track_tags, required_tags))
165     return 0;
166
167   /* Use the configured weight if available */
168   if((s = kvp_get(prefs, "weight"))) {
169     long n;
170     errno = 0;
171
172     n = strtol(s, 0, 10);
173     if((errno == 0 || errno == ERANGE) && n >= 0)
174       return n;
175   }
176
177   /* Bias up tracks that were recently added */
178   if((s = kvp_get(data, "_noticed"))) {
179     const time_t noticed = atoll(s);
180
181     if(noticed + config->new_bias_age < now)
182       /* Currently we just step up the weight of tracks that are in range.  A
183        * more sophisticated approach would be to linearly decay from new_bias
184        * down to BASE_WEIGHT over the course of the new_bias_age interval
185        * starting when the track is added. */
186       return config->new_bias;
187   }
188   
189   return BASE_WEIGHT;
190 }
191
192 static unsigned char random_buffer[4096];
193 static size_t random_left;
194
195 /** @brief Fill [buf, buf+n) with random bytes */
196 static void random_bytes(unsigned char *buf, size_t n) {
197   while(n > 0) {
198     if(random_left > 0) {
199       const size_t this_time = n > random_left ? random_left : n;
200
201       memcpy(buf, random_buffer + random_left - this_time, this_time);
202       n -= this_time;
203       random_left -= this_time;
204     } else {
205       static int fd = -1;
206       int r;
207
208       if(fd < 0) {
209         if((fd = open("/dev/urandom", O_RDONLY)) < 0)
210           fatal(errno, "opening /dev/urandom");
211       }
212       if((r = read(fd, random_buffer, sizeof random_buffer)) < 0)
213         fatal(errno, "reading /dev/urandom");
214       if((size_t)r < sizeof random_buffer)
215         fatal(0, "short read from /dev/urandom");
216       random_left = sizeof random_buffer;
217     }
218   }
219 }
220
221 /** @brief Pick a random integer uniformly from [0, limit) */
222 static unsigned long long pick_weight(unsigned long long limit) {
223   unsigned char buf[(sizeof(unsigned long long) * CHAR_BIT + 7)/8], m;
224   unsigned long long t, r, slop;
225   int i, nby, nbi;
226
227   //info("pick_weight: limit = %llu", limit);
228
229   /* First, decide how many bits of output we actually need; do bytes first
230    * (they're quicker) and then bits.
231    *
232    * To speed this up, we could use a binary search if we knew where to
233    * start.  (Note that shifting by ULLONG_BITS or more (if such a constant
234    * existed) is undefined behaviour, so we mustn't do that.)  Figuring out a
235    * start point involves preprocessor and/or autoconf magic.
236    */
237   for (nby = 1, t = (limit - 1) >> 8; t; nby++, t >>= 8)
238     ;
239   nbi = (nby - 1) << 3; t = limit >> nbi;
240   if (t >> 4) { t >>= 4; nbi += 4; }
241   if (t >> 2) { t >>= 2; nbi += 2; }
242   if (t >> 1) { t >>= 1; nbi += 1; }
243   nbi++;
244   //info("nby = %d; nbi = %d", nby, nbi);
245
246   /* Main randomness collection loop.  We read a number of bytes from the
247    * randomness source, and glue them together into an integer (dropping
248    * bits off the top byte as necessary).  Call the result r; we have
249    * 2^{nbi - 1) <= limit < 2^nbi and r < 2^nbi.  If r < limit then we win;
250    * otherwise we try again.  Given the above bounds, we expect fewer than 2
251    * iterations.
252    *
253    * Unfortunately there are subtleties.  In particular, 2^nbi may in fact be
254    * zero due to overflow.  So in fact what we do is compute slop = 2^nbi -
255    * limit > 0; if r < slop then we try again, otherwise r - slop is our
256    * winner.
257    */
258   slop = (2 << (nbi - 1)) - limit;
259   m = nbi & 7 ? (1 << (nbi & 7)) - 1 : 0xff;
260   //info("slop = %llu", slop);
261   //info("m = 0x%02x", m);
262
263   do {
264     /* Actually get some random data. */
265     random_bytes(buf, nby);
266
267     /* Clobber the top byte.  */
268     buf[0] &= m;
269
270     /* Turn it into an integer.  */
271     for (r = 0, i = 0; i < nby; i++)
272       r = (r << 8) | buf[i];
273     //info("r = %llu", r);
274   } while (r < slop);
275
276   return r - slop;
277 }
278
279 /** @brief Called for each track */
280 static int collect_tracks_callback(const char *track,
281                                    struct kvp *data,
282                                    struct kvp *prefs,
283                                    void attribute((unused)) *u,
284                                    DB_TXN attribute((unused)) *tid) {
285   unsigned long weight = compute_weight(track, data, prefs);
286
287   /* Decide whether this is the winning track.
288    *
289    * Suppose that we have n things, and thing i, for 0 <= i < n, has weight
290    * w_i.  Let c_i = w_0 + ... + w_{i-1} be the cumulative weight of the
291    * things previous to thing i, and let W = c_n = w_0 + ... + w_{i-1} be the
292    * total weight.  We can clearly choose a random thing with the correct
293    * weightings by picking a random number r in [0, W) and chooeing thing i
294    * where c_i <= r < c_i + w_i.  But this involves having an enormous list
295    * and taking two passes over it (which has bad locality and is ugly).
296    *
297    * Here's another way.  Initialize v = -1.  Examine the things in order;
298    * for thing i, choose a random number r_i in [0, c_i + w_i).  If r_i < w_i
299    * then set v <- i.
300    *
301    * Claim.  For all 0 <= i < n, the above algorithm chooses thing i with
302    * probability w_i/W.
303    *
304    * Proof.  Induction on n.   The claim is clear for n = 1.  Suppose it's
305    * true for n - 1.  Let L be the event that we choose thing n - 1.  Clearly
306    * Pr[L] = w_{n-1}/W.  Condition on not-L: then the probabilty that we
307    * choose thing i, for 0 <= i < n - 1, is w_i/c_{n-1} (induction
308    * hypothesis); undoing the conditioning gives the desired result.
309    */
310   if(weight) {
311     total_weight += weight;
312     if (pick_weight(total_weight) < weight)
313       winning = track;
314   }
315   ntracks++;
316   return 0;
317 }
318
319 int main(int argc, char **argv) {
320   int n, logsyslog = !isatty(2), err;
321   const char *tags;
322   
323   set_progname(argv);
324   mem_init();
325   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
326   while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
327     switch(n) {
328     case 'h': help();
329     case 'V': version("disorder-choose");
330     case 'c': configfile = optarg; break;
331     case 'd': debugging = 1; break;
332     case 'D': debugging = 0; break;
333     case 'S': logsyslog = 0; break;
334     case 's': logsyslog = 1; break;
335     default: fatal(0, "invalid option");
336     }
337   }
338   if(logsyslog) {
339     openlog(progname, LOG_PID, LOG_DAEMON);
340     log_default = &log_syslog;
341   }
342   if(config_read(0)) fatal(0, "cannot read configuration");
343   /* Find out current queue/recent list */
344   queue_read();
345   recent_read();
346   /* Generate the candidate track list */
347   trackdb_init(TRACKDB_NO_RECOVER);
348   trackdb_open(TRACKDB_NO_UPGRADE|TRACKDB_READ_ONLY);
349   global_tid = trackdb_begin_transaction();
350   if((err = trackdb_get_global_tid("required-tags", global_tid, &tags)))
351     fatal(0, "error getting required-tags: %s", db_strerror(err));
352   required_tags = parsetags(tags);
353   if((err = trackdb_get_global_tid("prohibited-tags", global_tid, &tags)))
354     fatal(0, "error getting prohibited-tags: %s", db_strerror(err));
355   prohibited_tags = parsetags(tags);
356   if(trackdb_scan(0, collect_tracks_callback, 0, global_tid))
357     exit(1);
358   trackdb_commit_transaction(global_tid);
359   trackdb_close();
360   trackdb_deinit();
361   //info("ntracks=%ld total_weight=%lld", ntracks, total_weight);
362   if(!total_weight)
363     fatal(0, "no tracks match random choice criteria");
364   if(!winning)
365     fatal(0, "internal: failed to pick a track");
366   /* Pick a track */
367   xprintf("%s", winning);
368   xfclose(stdout);
369   return 0;
370 }
371
372 /*
373 Local Variables:
374 c-basic-offset:2
375 comment-column:40
376 fill-column:79
377 indent-tabs-mode:nil
378 End:
379 */