0

To start off I have two processes that are running concurrently that support each other. One process reads a simple flatfile which contains snapshots of data separated by timestamps. This application simply opens this file (without file locking), reads a snapshot and places it into a another file called topology.netviz (with file locking). The second application reads topology.netziv (with file locking) and transfers the data into a temporary file to reduce latency of the programming holding the lock between the other process.

My problem is as simple as stated: When I transfer the data to the temporary file in the second process, weird characters/corrupted data is transferred. I've provided some code below to allow you guys to get a feel for what might be the issue.

Process 1:

try {
    // Determine if File Exists
    topologyFile = new File(Settings.NODE_TOPOLOGY_PATH);
    if (!topologyFile.exists())
        topologyFile.createNewFile();

    // FileChannel Gives the Ability to Create a File Lock
    FileChannel channel =
        new RandomAccessFile(topologyFile, "rwd").getChannel();

    // Use the FileChannel to Create a Lock on the 'distance.dat' (Blocking Method)
    FileLock lock = channel.lock();

    // Delete Files Contents
    channel.truncate(0);

    // Convert 'data' into ByteBuffer
    ByteBuffer buffer = ByteBuffer.wrap(data);

    // Write 'buffer' to 'channel'
    channel.write(buffer, 0);

    // Release Lock
    lock.release();

    // Close Channel
    channel.close();
}

catch(IOException error)
{
    System.out.println("Topology Thread: FileChannel; I/O Error Occured");
}

catch(NonWritableChannelException error)
{
    System.
        out.println("Topology Thread: FileChannel; File is not Writeable");
}

Process 2:

try {
    // Determine if File Exists
    topologyFileTemp = new File("tmp/topology.dat");
    if (topologyFileTemp.exists())
        topologyFileTemp.delete();  // Should Never Occur Unless Program Crashes

    // Recreate 'topologyFileTemp'
    topologyFileTemp = new File("tmp/topology.dat");
    topologyFileTemp.createNewFile();

    // Determine if File Exists
    topologyFile = new File("topology.netviz");
    if (!topologyFile.exists())
        topologyFile.createNewFile();   // Should Never Occur

    // Initialize Data Container from 'topology.netviz' in the Form of Bytes
    ByteBuffer topologyData =
        ByteBuffer.allocate((int)topologyFile.length());

    // FileChannel Gives the Ability to Create a File Lock for 'topology.netviz'
    FileChannel rChannel =
        new RandomAccessFile(topologyFile, "rwd").getChannel();

    // Use the FileChannel to Create a Lock on the 'distance.dat' (Blocking Method)
    FileLock lock = rChannel.lock();

    // Grab Data from 'topology.netviz'
    rChannel.read(topologyData);

    // Release Lock
    lock.release();

    // Close Channel
    rChannel.close();

    // FileChannel Gives the Ability to Create a File Lock for 'tmp/topology.dat'
    FileChannel wChannel =
        new RandomAccessFile(topologyFileTemp, "rw").getChannel();

    // Reset Buffers Position
    topologyData.position(0);

    // Write 'topologyData' to 'tmp/topology.dat'
    wChannel.write(topologyData);

    // Close the file
    wChannel.close();
}

catch(IOException error)
{
    System.out.println("Topology Thread: FileChannel; I/O Error Occured");
}

catch(NonWritableChannelException error)
{
    System.out.
        println("Topology Thread: FileChannel; File is not Writeable");
}
sarnold
  • 102,305
  • 22
  • 181
  • 238
Mitchell Romanuik
  • 193
  • 1
  • 1
  • 7

1 Answers1

0

One potential problem is that Process #1 is releasing the lock before it closes the channel. Since closing the channel flushes outstanding writes, this may mean that data is being written after the lock has been released, and that could cause corruption.

Another possibility is that the bytes are corrupted before they are written ... or there is a mismatch between the formats used by the writer and the reader.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216