<commit_before>package com.neverwinterdp.scribengin.storage.hdfs;

import java.io.IOException;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException;
import com.neverwinterdp.util.JSONSerializer;

public class SegmentLock {
  private FileSystem             fs;
  private Path                   lockPath;
  private SegmentOperationConfig operationConfig;
  transient private boolean      owner = false;
  
  public SegmentLock(FileSystem fs, Path lockPath, SegmentOperationConfig operationConfig) {
    this.fs = fs ;
    this.lockPath = lockPath;
    this.operationConfig = operationConfig;
  }

  public Path getLockPath() {  return lockPath; }

  public SegmentOperationConfig getOperationConfig() { return operationConfig; }

  public boolean tryLock(long timeout, long tryPeriod) throws IOException, InterruptedException {
    long stopTime = System.currentTimeMillis() + timeout;
    while(stopTime > System.currentTimeMillis()) {
      boolean locked = lock() ;
      if(locked) return true;
      Thread.sleep(tryPeriod);
    }
    return false; 
  }
  
  synchronized public void update(SegmentOperationConfig config) throws IOException {
    checkOwner();
    FSDataOutputStream out = fs.create(lockPath, true);
    byte[] bytes = JSONSerializer.INSTANCE.toBytes(config);
    out.write(bytes);
    out.hflush();
    out.close();
  }
  
  synchronized public boolean lock() throws IOException {
    if(owner) return true;
    try {
      if(fs.exists(lockPath)) return false ;
      FSDataOutputStream out = fs.create(lockPath, false);
      out.hsync();
      byte[] bytes = JSONSerializer.INSTANCE.toBytes(operationConfig);
      out.write(bytes);
      out.hflush();
      out.close();
      owner = true;
      return owner;
    } catch(AlreadyBeingCreatedException ex) {
      System.err.println("Lock Error: " + ex.getMessage());
      return false;
    } catch(IOException ex) {
      System.err.println("Lock Error: " + ex.getMessage());
      if(ex.getMessage().startsWith("File already exists")) {
        return false;
      } else if(ex instanceof AlreadyBeingCreatedException) {
        System.err.println("Lock Error(IOException): " + ex.getMessage());
        return false;
      }
      throw ex;
    }
  }
  
  synchronized public boolean unlock() throws IOException {
    checkOwner();
    boolean deleted = fs.delete(lockPath, false);
    owner = !deleted;
    //System.out.println("<== unlock, owner = " + owner + "\n");
    return deleted;
  }
  <commit_msg>try to fix and add more debug for lock fail<commit_after>package com.neverwinterdp.scribengin.storage.hdfs;

import java.io.IOException;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException;
import com.neverwinterdp.util.JSONSerializer;

public class SegmentLock {
  private FileSystem             fs;
  private Path                   lockPath;
  private SegmentOperationConfig operationConfig;
  transient private boolean      owner = false;
  
  public SegmentLock(FileSystem fs, Path lockPath, SegmentOperationConfig operationConfig) {
    this.fs = fs ;
    this.lockPath = lockPath;
    this.operationConfig = operationConfig;
  }

  public Path getLockPath() {  return lockPath; }

  public SegmentOperationConfig getOperationConfig() { return operationConfig; }

  public boolean tryLock(long timeout, long tryPeriod) throws IOException, InterruptedException {
    long stopTime = System.currentTimeMillis() + timeout;
    while(stopTime > System.currentTimeMillis()) {
      boolean locked = lock() ;
      if(locked) return true;
      Thread.sleep(tryPeriod);
    }
    return false; 
  }
  
  synchronized public void update(SegmentOperationConfig config) throws IOException {
    checkOwner();
    FSDataOutputStream out = fs.create(lockPath, true);
    byte[] bytes = JSONSerializer.INSTANCE.toBytes(config);
    out.write(bytes);
    out.hflush();
    out.close();
  }
  
  synchronized public boolean lock() throws IOException {
    if(owner) return true;
    try {
      if(fs.exists(lockPath)) return false ;
      FSDataOutputStream out = fs.create(lockPath, false);
      out.hsync();
      byte[] bytes = JSONSerializer.INSTANCE.toBytes(operationConfig);
      out.write(bytes);
      out.hflush();
      out.close();
      owner = true;
      return owner;
    } catch(IOException ex) {
      if(ex.getMessage().startsWith("File already exists")) {
        System.err.println("Lock Error(File already exists): " + ex.getMessage());
        return false;
      } else if(ex.getMessage().indexOf("Failed to CREATE_FILE") >=0 ) {
        System.err.println("Lock Error(Failed to CREATE_FILE): " + ex.getMessage());
        return false;
      }
      System.err.println("Lock Error Exception Type: " + ex.getClass().getName());
      System.err.println("Lock Error(Unknown): " + ex.getMessage());
      throw ex;
    }
  }
  
  synchronized public boolean unlock() throws IOException {
    checkOwner();
    boolean deleted = fs.delete(lockPath, false);
    owner = !deleted;
    //System.out.println("<== unlock, owner = " + owner + "\n");
    return deleted;
  }
  
{'fraction_non_alphanumeric': 0.10996820647091828, 'fraction_numerical': 0.0001870207593042828, 'mean_word_length': 3.3163841807909606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3025278', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1501, 'n_words': 359}