Comment 29 for bug 1398078

Revision history for this message
zhangsong (zhangsong) wrote :

The current code will retry if it encounters the error, but this is a work-around, not the root cause.

Current code of tgt.py use command 'tgt-admin --update name' to create the iscsi target of tgt:
https://github.com/openstack/cinder/blob/master/cinder/volume/targets/tgt.py#L119

The workflow of tgt-admin is:
1. get the available tid(usually it is the current max tid plus 1),I named it next_tid ;
2. use the next_tid to create a target:
tgtadm -C 0 --lld iscsi --op new --mode target --tid next_tid -T target_name

Here is the source code of tgt-admin:
https://github.com/fujita/tgt/blob/master/scripts/tgt-admin#L278

But there is a race condition between step 1 and step 2. If multiple tgt-admin commands executed simultaneous in the same node, some of them have the chance to get the same next_tid, and then use the same tid to create target, this will cause the error of 'tgtadm: this target already exists'.

I think the best way to solve this problem is to add a lock when create a tgt target.