CP - Proposed Solution (Java)
The Java component of the proposed solution has a number of considerations which must be addressed.
(Writing) Generating Unique CID
The problem states that each log entry must have an immutable key: a unique Commit Identification (CID). I propose using a two-part CID, separated with a hyphen ("-") for human readability. It will contain two parts. Part one is the current Linux timestamp. Part two is a unique, random string, based on a psuedo-random method in Java that takes advantage (at the core) of the Java random library call. The timestamp guarantees that if two writes happen at least one second apart, they will have unique values. If, however, two writes occur within the same second, the psuedo-random suffix will make the CID unique. There is a theoretical possibility that the same CID could be produced, but the probability of this occurring is small enough to ignore.
I note that the problem did not require sequential identifiers. If this were the case, then the writing class would need to store and increment the identifier using some persistent storage method.
I further note that this solution has the advantage of embedding the time and date which each log message was created. This has the effect of (somewhat) future-proofing the log entries. If, in the future it becomes important to display when each entry was made, or the time delta between entries, the timestamp artifact is available.
(Writing) Race condition on file save
If two writers attempt to write to the same logfile simultaneously, it is possible that they would collide during the write call. This could result in malformed records, because two lines from the file would be intermixed. As a practical matter, Linux prevents this, since Java file writes under a configurable size are atomic. That is, they are guaranteed to complete before another process resumes its context. However, the problem statement did not specify Linux, and other platforms may or may not support this native feature.
For this reason, I have "wrapped" the actual writing of the log message within a Java semaphore. By virtue of Java semaphores, if two instances of the call collide, one is guaranteed to wait until the other completes. The danger with semaphores is two-fold. First, it can impact performance. To limit the impact of reducing the system to a single bottleneck (the parts within the "wrapper"), I have excluded all functions except the actual write call. Everything else can be handled simultaneously without damage. The second danger is "deadly embrace." This is not possible here because there is a single scarce resource (one semaphore), so the "first" process cannot be waiting on the "second" to complete anything (at the same time that the "second" process is waiting for the "first" to free the semaphore).
One thing that has been omitted in the code for clarity sake is handling an Interrupt Exception in the write. I have included a "finally" block which tidies up on close, and I rely on this to address any open resources in the event of an Interrupt (rather than handling the case in the call itself).
(Reading) Race condition on read
It is conceivable that a Reader will begin reading the logfile during a Writer file save. In this event, it is possible for the Reader to receive a malformed (partial) record. For clarity, I have omitted anything to handle this. One solution would be to create a "lock" file in the writer before any file save, and remove it when complete. It would be wise to store the process id of the writer process in the lock file, so that "orphaned" lock files could be removed automatically on a subsequent access by either the reader or writer. This is the condition where a writer is interrupted after creating the lock, but before completing its file save. In this case, the lock file would be left in place, even though the process that created ended. If the process id is stored in the lock file, the writer/reader could check for the existence of that process prior to waiting on the lock. If the process did not exist, the writer/reader could proceed. As a side-note, using an external lock file in addition to a semaphore has the potential to create a deadly-embrace danger mentioned above.
Implementation Details
The problem suggests, but does not require, two command-line "programs" which can be executed from the shell. I have followed this suggestion, and created two classes, ReaderCmd and WriterCmd which contain the implementation details for the command line. I have isolated these details from the classes which actually answer the Problem Statement to improve re-use and simplify testing. I have used a third-party library Commons CLI. Other than this library, the programs do not rely on non-core Java libraries.
The implementation follows the "GNU" format, of single-hyphen command line options. For the WriterCmd there are three, for ReaderCmd there are two. For the Writer, the command options are:
-fname <FNAME> where FNAME is the name of the logfile to be written to -cid <CID> where CID is the identifier of the writer -msg <MSG> where MSG is the actual text message to log
For the Reader, the command options are:
-fname <FNAME> where FNAME is the name of the logfile to be read from -cid <CID> where CID is the identifier of the writer