If it's mid-lesson and you can sense it's not going as planned, abort mission! It is OK to change your plan. If you realize the students are lacking prior knowledge, don't try to fit in that prerequisite skill and the lesson you had planned to teach in one day. Surely that will not work. Rather, improvise. Tell the kids you realize they aren't quite ready for the lesson you planned, so you're going to redirect the way things are going to meet their current needs, and you'll continue the lesson when they are ready.
If the failure was due to poor classroom management, consider a different style of teaching. If you rely on group work but realize your students socialize while in groups, change your seating. Make rows and see if that helps. If group work results in little results, consider giving more independent practice. Or, set a timer and let the students know that if they can complete the first half of the assignment in 10 minutes you will allow them to work with their table groups for the last 10 minutes of class.