Reputation: 31
I am using multi-thread perl script to add entry to a ldap server.The script is reading some files and then making the corresponding entry to the ldap database. My script is following:
#!/usr/bin/perl -w
use strict;
use warnings;
use threads;
use threads::shared;
#use Thread::Queue;
use POSIX;
use Data::Dumper;
use Net::LDAP;
use Net::LDAP::LDIF;
use Net::LDAP::Entry;
#my $q = Thread::Queue->new(); # A new empty queue
my $INPUT_DIR="/home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/";
opendir(DIR, $INPUT_DIR) ;
my @files = grep { /^InputFile/ } readdir DIR;
my $count = @files;
print "Total Files: $count \n";
my @threads;
my $noofthread = 3;
my $nooffiles = $count;
my $noofbatch = $nooffiles / $noofthread;
print "No of batch1 : $noofbatch \n";
my $abc = $nooffiles % $noofthread;
print "ABC: $abc \n";
if (($nooffiles % $noofthread) > 0) {
$noofbatch = ceil($noofbatch);
}
print "No of batch2: $noofbatch \n";
my $ldap = '';
## Connect and bind to the server.
$ldap = Net::LDAP->new( '6873ZR1.egi.ericsson.com',
port => 389,
version => 3,
verify => 'optional',
cafile => '/etc/ldap/cacert.pem' ) or die $!;
## Bind to the server. The account must have sufficient privileges because you will
## be adding new entries.
my $result = $ldap->bind("administratorName=***,nodeName=***",
password => "***");
die $result->error() if $result->code();
# split the workload into N batches
#
while (my @batch = splice(@files, 0, $noofbatch)) {
#print "@batch \n";
push @threads, threads->new(\&doOperation, @batch);
}
for my $thr (@threads) {
#print "Ending Thread: $thr \n";
$thr->join;
}
sub doOperation () {
my $ithread = threads->tid() ;
print "Thread Index : [id=$ithread]\n" ;
my $INPUT_DIR="/home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/";
foreach my $item (@_) {
my $filename = $INPUT_DIR.$item;
#print "Filename: $filename \n";
die "$filename not found!\n" unless ( -f $filename );
my $ldif = Net::LDAP::LDIF->new ($filename, "r") or die $!;
while ( ! $ldif->eof()) {
my $entry = $ldif->read_entry();
$result = $ldap->add($entry);
$result = $entry->add()->update( $ldap );
print "$filename : $result \n";
}
}
}
Upto the code $result = $ldap->add($entry);
its working fine. But while its coming to the line $result = $entry->add()->update( $ldap );
it's showing the following error:
eyacdel@6873ZR1:~/Documents/MM/Roger_SAPC/myscript$ perl thread_test1.pl
Total Files: 10
No of batch1 : 3.33333333333333
ABC: 1
No of batch2: 4
Thread Index : [id=1]
Thread Index : [id=2]
1 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF /InputFile1_5.ldif : Net::LDAP::Add=HASH(0x7fe86c006ba0)
Thread Index : [id=3]
2 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_5.ldif : Net::LDAP::Add=HASH(0x7fe86c006cf0)
3 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_5.ldif : Net::LDAP::Add=HASH(0x7fe86c012568)
1 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_4.ldif : Net::LDAP::Add=HASH(0x7fe868007e90)
1 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_6.ldif : Net::LDAP::Add=HASH(0x7fe864006ba0)
4 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_5.ldif : Net::LDAP::Add=HASH(0x161e500)
2 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_4.ldif : Net::LDAP::Add=HASH(0x7fe868007fe0)
2 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_6.ldif : Net::LDAP::Add=HASH(0x7fe864006cf0)
1 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_10.ldif : Net::LDAP::Add=HASH(0x161e4b8)
3 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_4.ldif : Net::LDAP::Add=HASH(0x7fe868012600)
2 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_10.ldif : Net::LDAP::Add=HASH(0x7fe86c006bd0)
3 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_6.ldif : Net::LDAP::Add=HASH(0x7fe864012640)
3 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_10.ldif : Net::LDAP::Add=HASH(0x7fe86c004ed0)
4 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_4.ldif : Net::LDAP::Add=HASH(0x7fe8680126d8)
4 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_6.ldif : Net::LDAP::Add=HASH(0x7fe864012718)
5 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_4.ldif : Net::LDAP::Add=HASH(0x7fe8680127b0)
4 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_10.ldif : Net::LDAP::Add=HASH(0x7fe86c004fa8)
5 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_10.ldif : Net::LDAP::Add=HASH(0x7fe86c004e10)
1 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_1.ldif : Net::LDAP::Add=HASH(0x161e608)
1 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_2.ldif : Net::LDAP::Add=HASH(0x1d6e9d8)
5 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_6.ldif : Net::LDAP::Add=HASH(0x7fe864014d60)
2 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_2.ldif : Net::LDAP::Add=HASH(0x7fe868007e30)
6 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_6.ldif : Net::LDAP::Add=HASH(0x7fe8640152c0)
3 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_2.ldif : Net::LDAP::Add=HASH(0x7fe868012810)
2 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_1.ldif : Net::LDAP::Add=HASH(0x7fe86c006bd0)
4 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_2.ldif : Net::LDAP::Add=HASH(0x7fe868015398)
3 --> /home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/InputFile1_1.ldif : Net::LDAP::Add=HASH(0x7fe86c004ea0)
Please help me out how to solve this issue.
Upvotes: 0
Views: 162
Reputation: 5069
First, it is not error, jut an output of this script. Could you paste the actual error?
You are printing out an object. Use Data::Dumper for that. If you interesting in the DN just added use the below.
Net::Ldap is not thread safe. You should create a new connection for every thread or you have to use some kind of blocking to make sure only one thread use it any given time.
Please use proper error checking if you are working with Net::Ldap or setup on_error => 'die' in the connection string.
change doOperation like this:
sub doOperation () {
my $ithread = threads->tid() ;
print "Thread Index : [id=$ithread]\n" ;
my $INPUT_DIR="/home/eyacdel/Documents/MM/Roger_SAPC/myscript/IMPORTLDIF/";
foreach my $item (@_) {
my $filename = $INPUT_DIR.$item;
#print "Filename: $filename \n";
die "$filename not found!\n" unless ( -f $filename );
my $ldap = connect_ldap();### Create a sub for ldap connect
my $ldif = Net::LDAP::LDIF->new ($filename, "r") or die $!;
while ( ! $ldif->eof()) {
my $entry = $ldif->read_entry();
$result = $ldap->add($entry);
$result = $entry->add()->update( $ldap );
print "tid: $ithread\t$filename: ".$result->code."\tdn: ".$entry-dn()."\n";
die "Error: ".$result->code() if $result->code();
}
$ldif->done();
$ldap->disconnect();
}
}
Upvotes: 2