Skip to contentSkip to conversation

the evils of the CLD api

DISCUSSION Colin McCabe 3 participants 8 messages Jan 16, 2010 – Jan 19, 2010 Library code

lore
Share & tools
lore
#0 lore raw

Hi all,

I saw Pete's post about "the evils of event-driven programming in CLD clients."

I tend to agree with him that the current API can be frustrating to think about. I had a few thoughts about what might be a better API:

1) when starting the cld session, the user code could register a "vtable" containing a bunch of different callbacks. That way all GET responses for the session could flow through my_get_response(), and so on.

This at least would give the API user a single place to look when trying to find out how some CLD event is handled.

Right now, we have callbacks installing other callbacks, which makes things hard to follow, IMHO.

2) provide a blocking API that users could take advantage of. I think for a lot of simple programs, programmers would be happy to spawn a thread per CLD session. In fact, I'm not aware of any programs that want to spawn a lot of cld sessions (maybe someone can point out one.)

Most of the CLD uses that I've seen have been things like "find out who the master node is." For that kind of use, it seems unlikely that you would open too many sessions. In an era of multi-core processors and NPTL, spawning a dedicated thread to find the master is kind of a big yawn.

I think a non-blocking API, in some form, should probably be available, though, in case people need it. We may not be able to anticipate all the uses that CLD is put to.

Anyway... those are just my thoughts right now...

Colin

#1 lore raw
Quoted 4 lines · On Fri, 15 Jan 2010 18:40:44 -0800 Colin McCabe <[email protected]> wrote:

1) when starting the cld session, the user code could register a "vtable" containing a bunch of different callbacks. That way all GET responses for the session could flow through my_get_response(), and so on.

I'm not sure if that would be much better than what we have now, so I don't see a big pay-off from switching to it.

Quoted 3 lines

I think a non-blocking API, in some form, should probably be available, though, in case people need it. We may not be able to anticipate all the uses that CLD is put to.

The easiest way to do this is to leave cldc intact.

Quoted 5 lines

2) provide a blocking API that users could take advantage of. I think for a lot of simple programs, programmers would be happy to spawn a thread per CLD session. In fact, I'm not aware of any programs that want to spawn a lot of cld sessions (maybe someone can point out one.)

Here's what I have for tonight:

commit e011b6c073d1dafd506dc36c5bb2d7ef122f65d7

Author: Master <[email protected]>
Date:   Fri Jan 15 23:26:08 2010 -0700

Checkpoint WIP for ncld.h.

include/ncld.h new file +58 -0
new file mode 100644
index 0000000..ed52f1d
@@ -0,0 +1,58 @@
+#ifndef __NCLD_H__
+#define __NCLD_H__
+
+/*
+ * Copyright 2009 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*
+ * The ncld.h API is a replacement for cldc.h. Do not include both.
+ *
+ * We do not believe into making "internal" structures "opaque"
+ * with pointers to void. Therefore, this header might want include
+ * some legacy definitions or whatnot, but users do not need to.
+ */
+#include <glib.h>
+#include <cldc.h>
+
+struct ncld_fh {
+};
+
+struct ncld_sess {
+	char *host;
+	unsigned short port;
+	// GMutex *mutex; mutex = g_mutex_new(); g_mutex_lock(mutex);
+	GThread *thread;
+	struct cldc_udp *udp;
+	void (*event)(struct ncld_sess *, void *, unsigned int);
+	void *event_arg;
+};
+
+extern struct ncld_sess *ncld_sess_open(const char *host, const char *port,
+	int *error,
+	void (*event)(struct ncld_sess *, void *, unsigned int), void *ev_arg);
+extern struct ncld_fh *ncld_open(struct ncld_sess *s, const char *fname,
+	unsigned int mode, int *error, unsigned int events,
+	void (*event)(struct ncld_fh *, void *, unsigned int), void *ev_arg);
+extern long ncld_read(struct ncld_fh *, void *data, long len);
+extern long ncld_write(struct ncld_fh *, const void *data, long len);
+extern int ncld_lock(struct ncld_fh *);
+extern void ncld_unlock(struct ncld_fh *);
+extern void ncld_close(struct ncld_fh *);
+extern void ncld_sess_close(struct ncld_sess *s);
+extern void ncld_init(void);
+
+#endif /* __NCLD_H__ */
lib/cldc.c +360 -0

366 lines not on this page · raw message

-- Pete

#2 lore raw

Looks promising...

Shouldn't ncld_unlock and ncld_close return an error code rather than just eating all errors? The server certainly does sometimes send back error responses for these requests.

The event() callback seems like to lead to some complexity. For example, let's say I have a "main" thread that does ncld_open, and then ncld_write. But sometime after the ncld_open succeeds, the session goes away. This triggers the event() callback to happen in the context of the "extra thread." Now, somehow, the "extra" thread needs to notify the "main" thread that something has happened, the jig is up, and it's time to go back to square one and reopen the session. (choose your favorite metaphor.)

I guess the main thread could check an atomic variable before each ncldc operation (has_failed? is_gone?) Or a mutex-protected variable. Or you could just let the "main" thread continue to blunder on until it crashes into an error code, then check has_failed. (In this case, you obviously need to ensure that all the pointers that it's using, like the session pointer, continue to be valid until that point.) None of these options really seem satisfying.

Rather than the event callback, I almost feel like it would be better just to have an ncldc_poll function modelled on poll(). It would block until the previous operation either completed or failed, or some other event happened. This implies that the rest of the operations are nonblocking. You have to have a state machine in your program anyway-- you might as well keep it simple and all in one place. What do you guys think?

regards,
Colin

Quoted 25 lines · Pete Zaitcev
Quoted 4 lines · On Fri, 15 Jan 2010 18:40:44 -0800 Colin McCabe <[email protected]> wrote:

1) when starting the cld session, the user code could register a "vtable" containing a bunch of different callbacks. That way all GET responses for the session could flow through my_get_response(), and so on.

I'm not sure if that would be much better than what we have now, so I don't see a big pay-off from switching to it.

Quoted 3 lines

I think a non-blocking API, in some form, should probably be available, though, in case people need it. We may not be able to anticipate all the uses that CLD is put to.

The easiest way to do this is to leave cldc intact.

Quoted 5 lines

2) provide a blocking API that users could take advantage of. I think for a lot of simple programs, programmers would be happy to spawn a thread per CLD session. In fact, I'm not aware of any programs that want to spawn a lot of cld sessions (maybe someone can point out one.)

Here's what I have for tonight:

#3 lore raw
Quoted 3 lines · On Sat, 16 Jan 2010 13:38:26 -0800 Colin McCabe <[email protected]> wrote:

Shouldn't ncld_unlock and ncld_close return an error code rather than just eating all errors? The server certainly does sometimes send back error responses for these requests.

This is an old bone of contention. The problem is, suppose the application is cleaning up for exit. What is it supposed to do with the error code? It cannot even retry sanely.

Quoted 3 lines

The event() callback seems like to lead to some complexity. For example, let's say I have a "main" thread that does ncld_open, and then ncld_write. []

Indeed, it reopens the old problems somewhat. The plan is to see how ncld matches tabled and chunkd and perhaps tweak the API a bit.

Quoted 2 lines

I guess the main thread could check an atomic variable before each ncldc operation (has_failed? is_gone?) Or a mutex-protected variable.

That's pretty much what I expect from most command-line tools. The check is implicit in read/write/lock operations.

Quoted 2 lines

Rather than the event callback, I almost feel like it would be better just to have an ncldc_poll function modelled on poll(). []

If we do that, we're at the square 1 with respect to complexity reduction in a multi-thread environments. If someone wants that, they may continue to use cldc_foo.

As a reminder, my aim here is 2-fold:

1. Provide something that permits to use synchronous programming in cldu.c. I am having a big trouble modifying it, and I know that when we support slave mode, I'll need even more complex behaviour from it. It will crash on itself unless I implement ncld.

2. Make something that lures new contributors into a sense of simplicity. We keep saying that it's almost like a filesystem, only not quite POSIX. But the actual API is such that nobody will use it if he has an alternative. We need a pseudo-filesystem API to continue making these claims with a straight face.

Anything else is not a goal, at least thus far. And actually I am going to resist expanding ncld's scope too much and ruining the simplicity of the model. You want async, use cldc.

-- Pete

#4 lore raw
Quoted 10 lines · Pete Zaitcev
Quoted 3 lines · On Sat, 16 Jan 2010 13:38:26 -0800 Colin McCabe <[email protected]> wrote:

Shouldn't ncld_unlock and ncld_close return an error code rather than just eating all errors? The server certainly does sometimes send back error responses for these requests.

This is an old bone of contention. The problem is, suppose the application is cleaning up for exit. What is it supposed to do with the error code? It cannot even retry sanely.

If it's important enough for you to print out an error (*not* debug) message in the library, it should be important enough to pass along to the library user.

After all, not everyone might be using a command-line app. Some people might want to see the error reflected in a dialog box, etc.

Quoted 4 lines
Quoted 3 lines

The event() callback seems like to lead to some complexity. For example, let's say I have a "main" thread that does ncld_open, and then ncld_write. []

Quoted 8 lines

Indeed, it reopens the old problems somewhat. The plan is to see how ncld matches tabled and chunkd and perhaps tweak the API a bit.

Quoted 2 lines

I guess the main thread could check an atomic variable before each ncldc operation (has_failed? is_gone?) Or a mutex-protected variable.

That's pretty much what I expect from most command-line tools. The check is implicit in read/write/lock operations.

The check is built-into the library? That makes sense.

Also, shouldn't the check also be implicit in unlock and close? Another reason for them to return an error code. :)

Quoted 14 lines
Quoted 2 lines

Rather than the event callback, I almost feel like it would be better just to have an ncldc_poll function modelled on poll(). []

If we do that, we're at the square 1 with respect to complexity reduction in a multi-thread environments. If someone wants that, they may continue to use cldc_foo.

As a reminder, my aim here is 2-fold:

1. Provide something that permits to use synchronous programming in cldu.c. I am having a big trouble modifying it, and I know that when we support slave mode, I'll need even more complex behaviour from it. It will crash on itself unless I implement ncld.

Quoted 5 lines

2. Make something that lures new contributors into a sense of simplicity. We keep saying that it's almost like a filesystem, only not quite POSIX. But the actual API is such that nobody will use it if he has an alternative. We need a pseudo-filesystem API to continue making these claims with a straight face.

I think this is a reasonable API. It's nice to see a more familiar face on read, write, etc. I was just trying to point out another path, one you might not have thought of.

Colin

#5 lore raw
Quoted 10 lines · On Sat, 16 Jan 2010 17:54:36 -0800 Colin McCabe <[email protected]> wrote:
Quoted 7 lines · Pete Zaitcev
Quoted 3 lines

Shouldn't ncld_unlock and ncld_close return an error code rather than just eating all errors? The server certainly does sometimes send back error responses for these requests.

This is an old bone of contention. The problem is, suppose the application is cleaning up for exit. What is it supposed to do with the error code? It cannot even retry sanely.

Quoted 6 lines

If it's important enough for you to print out an error (*not* debug) message in the library, it should be important enough to pass along to the library user.

After all, not everyone might be using a command-line app. Some people might want to see the error reflected in a dialog box, etc.

I'm going to try and explain again why the tear-down calls must not return operation errors (they are free to return programmatic errors). This is an important point for API design.

Suppose for a moment that ncld_end_sess returns an error. What is your application going to do about it?

You seem to be suggesting to pop a dialog. But what is in the dialog for the user? What can a user do? The answer is: a) retry, b) resign to the loss of data and terminate the application forcefuly in the OS. Or, actually, first (a) and then (b). And for the (b) to work above OS you'll have a forceful tead-down method anyway (or rely on OS).

What's more, some applications do not have a way to interact with a human (e.g. tabled). And this is where you got the logics completely wrong. When I pointed that requirements of such applications dictate a call that unconditionally ends the life of a structure or handle, you (correctly) pointed out that interactive applications exist too. But what of it?! The API must satisfy both kinds!

The correct approach -- and only if you _really_ have no alternative to have some data outstanding or have other server interaction on close -- is to provide a separate "flush" method, which can fail. Then, the retry dialogs or other methods (e.g. copying of data into other handle) can be used sanely.

-- Pete

P.S. ncld does not have a flush method because its writes are synchronous. And if we cannot contact a server to close a session, it does not matter. So the whole discussion is moot really. Nonetheless I think it's important to have it, in general sense.

#6 lore raw
In reply to Colin McCabe
Quoted 1 line · On Sat, 16 Jan 2010 13:38:26 -0800 Colin McCabe <[email protected]> wrote:

Looks promising...

Here's a version that works:

include/ncld.h new file +88 -0
new file mode 100644
index 0000000..067e6ff
@@ -0,0 +1,88 @@
+#ifndef __NCLD_H__
+#define __NCLD_H__
+
+/*
+ * Copyright 2009 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*
+ * The ncld.h API is a replacement for cldc.h. Do not include both.
+ *
+ * We do not believe into making "internal" structures "opaque"
+ * with pointers to void. Therefore, this header might want include
+ * some legacy definitions or whatnot, but users do not need to.
+ */
+#include <stdbool.h>
+#include <glib.h>
+#include <cldc.h>
+#include <libtimer.h>
+
+struct ncld_sess {
+	char *host;
+	unsigned short port;
+	GMutex *mutex;
+	GCond *cond;
+	GThread *thread;
+	bool is_up;
+	int errc;
+	GList *handles;
+	int to_thread[2];
+	struct cldc_udp *udp;
+	struct cld_timer udp_timer;
+	struct cld_timer_list tlist;
+	void (*event)(void *, unsigned int);
+	void *event_arg;
+};
+
+struct ncld_fh {
+	struct ncld_sess *ses;
+	struct cldc_fh *fh;		/* FIXME cldc_open2 take direct & */
+	bool is_open;
+	int errc;
+	int nios;
+	unsigned int event_mask;
+	void (*event_func)(void *, unsigned int);
+	void *event_arg;
+};
+
+struct ncld_read {
+	/* public to application */
+	const void *ptr;
+	long length;
+
+	struct ncld_fh *fh;
+	/* GCond *cond;   -- abusing the conditional of file handle for now */
+	bool is_done;
+	int errc;
+};
+
+extern struct ncld_sess *ncld_sess_open(const char *host, const char *port,
+	int *error, void (*event)(void *, unsigned int), void *ev_arg,
+	const char *cld_user, const char *cld_key, struct hail_log *hlog);
+extern struct ncld_fh *ncld_open(struct ncld_sess *s, const char *fname,
+	unsigned int mode, int *error, unsigned int events,
+	void (*event)(void *, unsigned int), void *ev_arg);
+extern int ncld_del(struct ncld_sess *nsp, const char *fname);
+extern struct ncld_read *ncld_get(struct ncld_fh *fhp, int *error);
+extern void ncld_read_free(struct ncld_read *rp);
+extern int ncld_write(struct ncld_fh *, const void *data, long len);
+extern int ncld_trylock(struct ncld_fh *);
+extern int ncld_unlock(struct ncld_fh *);
+extern void ncld_close(struct ncld_fh *);
+extern void ncld_sess_close(struct ncld_sess *s);
+extern void ncld_init(void);
+
+#endif /* __NCLD_H__ */
lib/cldc.c +836 -2

861 lines not on this page · raw message

test/lock-file-event.c +51 -227

318 lines not on this page · raw message

I am going to work on this gradually and flip over cldcli, rest of tests, and cldu.c in chunkd and tabled, then we'll see how it works.

-- Pete

#7 lore raw
In reply to Colin McCabe

My general comment on the libcldc API is that it is a low-level API, and definitely not very user friendly at all. ncld or other APIs would improve life quite a bit. Making life easier for programmers, and cutting out tons of code, is a plus :)

Jeff, just returned from camping